Solved

error: class type redefinition ....

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

813 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