Solved

error: class type redefinition ....

Posted on 2004-10-12
3
307 Views
Last Modified: 2012-05-05
Hi Experts,

  I got a "class type redefinition" error in the following code. (in VC++). Does anyone know what did I do wrong ??? thanks !!!


// MeowObject.cpp: implementation of the MeowObject class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MeowObject.h"

class MeowObject{
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

private:
      int _x, _y ;

MeowObject::MeowObject()
{

}

MeowObject::~MeowObject()
{

}

public:
       void set_x(int x){_x =x ;}
       void set_y(int y){_y =y ;}
       int get_x(){return _x ;}
       int get_y(){return _y ;}

};
0
Comment
Question by:meow00
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 3

Assisted Solution

by:Indrawati
Indrawati earned 100 total points
ID: 12285318
// MeowObject.cpp: implementation of the MeowObject class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MeowObject.h"

class MeowObject{

---> Why do you give the class' declaration in the cpp file? What's the content of MeowObject.h then?

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

private:
     int _x, _y ;

MeowObject::MeowObject()

---> Under most circumstances, default constructor is usually public

{

}

MeowObject::~MeowObject()

---> Same with constructor, destructor is usually public

{

}

public:
       void set_x(int x){_x =x ;}
       void set_y(int y){_y =y ;}
       int get_x(){return _x ;}
       int get_y(){return _y ;}

};


Maybe it will be better if you just move all of it (from class MeowObject{ right to the closing };) to the .h file? It seems that your class can do without any .cpp file.
0
 
LVL 12

Expert Comment

by:stefan73
ID: 12285506
Hi meow00,
It looks like you're mixing up class definition and implementation.

Unlike in languages like Ruby, you cannot change the definition of a C++ class, once it is defined.

Do definitions like
private:
     int _x, _y ;

public:
       void set_x(int x){_x =x ;}
       void set_y(int y){_y =y ;}
       int get_x(){return _x ;}
       int get_y(){return _y ;}


in your .h file, and implement the methods like MeowObject::~MeowObject() and MeowObject::MeowObject() in your .cpp file. Leave out the "class MeowObject{ ... } " wrapper in your implementation.






Cheers!

Stefan
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 150 total points
ID: 12285521
Like:

MeowObject.h:
class MeowObject{
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

private:
     int _x, _y ;


public:
    MeowObject();
    ~MeowObject();
       void set_x(int x){_x =x ;}
       void set_y(int y){_y =y ;}
       int get_x(){return _x ;}
       int get_y(){return _y ;}

};

In MeowObject.cpp:
#include "stdafx.h"
#include "MeowObject.h"


MeowObject::MeowObject()
{

}

MeowObject::~MeowObject()
{

}

And be aware that a private constructor/destructor is only feasible for a singleton class. They're normally public or protected (for abstract classes).
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question