gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Class redefinition error...

#27555 - mr_square - Fri Oct 15, 2004 2:35 pm

Hi all - I'm having a bit of trouble with compiling my C++ code. Everthing was working fine until about 1/2 hour ago when I tried to create a new class, 'collisionDetector'. This class needs to use one of my other classes, 'playerState' , as it takes a playerState instance as an argument to one of its methods.

So I add #include "playerState.h" to the top of collisionDetector.h file, and it comes back with errors about a redefinition of the playerState class. My main.cpp file also includes the same file, but surely if its a header it should work??


Also - I was told that #include-ing C files is bad - I've compiled them into .o files like the Beginners FAQ says, but the compiler cant find their contents at compile time (they contain arrays, and the code that uses the arrays gets errors that they don't exist). I'm using:

Code:

path=C:\devkitadv\bin

g++ -o city.o citybackdrop.o main.elf defines.cpp main.cpp DMA.cpp drawState.cpp background.cpp idleState.cpp bg.cpp jumpState.cpp lookUpState.cpp runLeftState.cpp trig.cpp kneelState.cpp runRightState.cpp playerState.cpp -lm

objcopy -O binary main.elf main.bin

#27556 - poslundc - Fri Oct 15, 2004 2:51 pm

mr_square wrote:
Hi all - I'm having a bit of trouble with compiling my C++ code. Everthing was working fine until about 1/2 hour ago when I tried to create a new class, 'collisionDetector'. This class needs to use one of my other classes, 'playerState' , as it takes a playerState instance as an argument to one of its methods.

So I add #include "playerState.h" to the top of collisionDetector.h file, and it comes back with errors about a redefinition of the playerState class. My main.cpp file also includes the same file, but surely if its a header it should work??


Make sure you're guarding against multiple inclusion. This is what happens in the following scenario:

a.h:
Code:
class myClass {...};


b.h:
Code:
#include "a.h"


program.c:
Code:
#include "a.h"
#include "b.h"


... so myClass has been defined twice, which causes a compiler error.

To prevent this, all header files should have a guard in them such as the following:

a.h:
Code:
#ifndef __SOME_UNIQUE_NAME__
#define __SOME_UNIQUE_NAME__

class myClass {...};

#endif


This will prevent your header file from being #included more than once.

Quote:
Also - I was told that #include-ing C files is bad - I've compiled them into .o files like the Beginners FAQ says, but the compiler cant find their contents at compile time (they contain arrays, and the code that uses the arrays gets errors that they don't exist).


You also need to create header files to be #included into the C code. Only these header files don't have the actual data in them, just references to the data so the compiler knows what to do. The data isn't actually appended on until the linking stage (separate from compilation).

A typical header file might look like:

Code:
#ifndef __MY_DATA_REFERENCES__
#define __MY_DATA_REFERENCES__

extern const unsigned int myExternalData[];     // contents are in some other file, eg. myExternalData.c

#endif


Then #include this file into the program that needs to access the data in your C file.

Dan.

#27558 - mr_square - Fri Oct 15, 2004 3:20 pm

Thanks man - great explanation :)