Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

error: class type redefinition ....

Posted on 2004-10-12
3
Medium Priority
?
312 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 400 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 600 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

  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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

610 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