Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting this redefinition of class error?

Apologies for the code dump:

gameObject.cpp:

#include "gameObject.h" class gameObject {     private:     int x;     int y;     public:     gameObject()     {     x = 0;     y = 0;     }      gameObject(int inx, int iny)     {         x = inx;         y = iny;     }      ~gameObject()     {     //     }     int add()     {         return x+y;     } }; 

gameObject.h:

class gameObject {     private:     int x;     int y;     public:     gameObject();      gameObject(int inx, int iny);     ~gameObject();     int add(); }; 

Errors:

||=== terrac, Debug ===| C:\terrac\gameObject.cpp|4|error: redefinition of `class gameObject'| C:\terrac\gameObject.h|3|error: previous definition of `class gameObject'| ||=== Build finished: 2 errors, 0 warnings ===| 

I can't figure out what's wrong. Help?

like image 823
Dataflashsabot Avatar asked Sep 19 '10 16:09

Dataflashsabot


People also ask

How do you fix a class redefinition?

Error C2011 – 'ClassName' : 'class' type redefinition Common causes include circular referencing and including a header file more than once (see example below). How to fix it? You can fix this error by ensuring that the class you are including does not get included more than once.

What does redefinition mean in code?

No. int a = foo(); or int a = 3; , inside main() , is a new variable that is also called a . A redefinition is an attempt to redefine the same variable, e.g.: int a = 5; int a = 6; Also.

How do you name a type in C++?

C++ code. Use CamelCase for all names. Start types (such as classes, structs, and typedefs) with a capital letter, other names (functions, variables) with a lowercase letter.


1 Answers

You're defining the class in the header file, include the header file into a *.cpp file and define the class a second time because the first definition is dragged into the translation unit by the header file. But only one gameObject class definition is allowed per translation unit.

You actually don't need to define the class a second time just to implement the functions. Implement the functions like this:

#include "gameObject.h"  gameObject::gameObject(int inx, int iny) {     x = inx;     y = iny; }  int gameObject::add() {     return x+y; } 

etc

like image 129
sellibitze Avatar answered Oct 15 '22 07:10

sellibitze