Solved

error: class type redefinition ....

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  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 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 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.

746 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

12 Experts available now in Live!

Get 1:1 Help Now