Solved

error: class type redefinition ....

Posted on 2004-10-12
3
302 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

912 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now