Advertisement

C++ question

Started by August 06, 2000 03:28 PM
6 comments, last by JSCFaith 24 years, 4 months ago
Hey, Well I am having a problem, ok here it is. I created a structure like this: struct MAP_STRUCT { int tileID; int WalkAble; int BuildAble; int tileposx; int tileposy; int Flags; }map[WORLD_SIZEX][WORLD_SIZEY]; I use this for my map ans stuff. Well I tried to put this in a .h file so that all the .cpp files could use it, but I keep getting an error that says, gameutil.obj : error LNK2005: "struct MAP_STRUCT (* map)[76]" (?map@@3PAY0EM@UMAP_STRUCT@@A) already defined in gamemain.obj Debug/James ConQuest.exe : fatal error LNK1169: one or more multiply defined symbols found I only declared it in one place, but It still says that, if I move the structure into the .cpp instead of the .h it works, but then my other .cpp files that need to use it too can''t, what am I doing wrong? P.S this happens whenever I create a class or structure and try to put it in a .h file.
at the start of your .h file put

#ifndef MAPSTRUCTDEF
#define MAPSTRUCTDEF

and at the end of your .h file put

#endif

what this does is make sure that the .h file is processed once. The #ifdef at the start checks to see if MAPSTRUCTDEF (you could use any name you want) was defined. If not, it #define's it. The #endif at the end is just like the '}' of the #ifdef.

Hope this hopes.
Queasy.


**yeah thanks martee, i wasn't paying attention... it should be a #ifndef... note the 'n'.

sorry

Edited by - queasy on August 6, 2000 5:18:19 PM
Jonathan Makqueasy gamesgate 88[email=jon.mak@utoronto.ca]email[/email]
Advertisement
Shouldn''t that be an #ifndef??

Martee
Magnum Games
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
I still get the same errors, I checked to see if I declared it anywhere else and I didn''t.
It is because you are declaring an instance of that data type in a header file which you cannot do. Instead do something like this:

// header file
struct MAP_STRUCT {

int tileID;
int WalkAble;
int BuildAble;
int tileposx;
int tileposy;
int Flags;
};

extern MAP_STRUCT map[WORLD_SIZEX][WORLD_SIZEY];

// source file
MAP_STRUCT map[WORLD_SIZEX][WORLD_SIZEY];

With declaring the extern version in the header you are telling the compiler this variable is going to be declared in a source file somewhere. This will work as long as you actually declare it in a source file. Hope this helps!

Todd
The way you do it, there will be one map[][] variable in each .obj file.

This is one way to make it work....

You declare the map[][] array in one obj file. Structname map[][]

And in all other files you write #extern structname map[][]


//Peter... Hope it helps...
Advertisement
quote:
Queasy:

The #ifdef at the start checks to see if MAPSTRUCTDEF (you could use any name you want) was defined. If not, it #define''s it. The #endif at the end is just like the ''}'' of the #ifdef.


This only stops the file being processed more than once per object file. It doesn''t stop the same thing appearing in multiple object files (that''s why it''s a link error, not a compile error).
Thanks Todd Casey, what you said worked, Thanks guys.

This topic is closed to new replies.

Advertisement