Simple Question..
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.
#infdef _whatever_the_header_file_is_called__
#define _whatever_the_header_file_is_called__
// put your code here
#endif
Put that in the header files and it should work. And don't forget to change _whatever_the_header_file_is_called__ to something unique for every header file!
Edited by - Muzzafarath on August 6, 2000 3:43:54 PM
#define _whatever_the_header_file_is_called__
// put your code here
#endif
Put that in the header files and it should work. And don't forget to change _whatever_the_header_file_is_called__ to something unique for every header file!
Edited by - Muzzafarath on August 6, 2000 3:43:54 PM
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
You said to put #ifndef, but do you mean #infdef, well I tried both and both still give me the same error. Then I tried ifdef but then I got lots more errors.
Yeah, I mean #ifndef (if not defined). #ifdef is wrong What errors do you get when using #ifndef?
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
August 06, 2000 04:13 PM
Here is my whole .h file:
[sourc]
#ifndef GAME_INCLUDES_H
#define GAME_INCLUDES_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "ddutil.h"
// Funtions /////////////////////////////
void CreateMap(void);
void LoadMap(void);
unsigned long GetCpuSpeed();
void GetFrameRate(LPDIRECTDRAWSURFACE7 lpDDSBack, DWORD dwTime);
void DrawTiles(LPDIRECTDRAWSURFACE7 lpDDSBack, LPDIRECTDRAWSURFACE7 tilebmp, HWND main_window_handle, int WorldPosx, int WorldPosy);
int CheckMouse(RECT mouse, RECT button);
void FillMap();
int MouseTileOver(RECT mouse, int tileposx, int tileposy);
// Globals //////////////////////////////
static DWORD dwFrameTime;
static DWORD dwFrames;
static DWORD dwFrameCount;
// Defines //////////////////////////////
#define WINDOW_CLASS_NAME "WINCLASS1"
#define TILE_WIDTH 64
#define TILE_HEIGHT 89
#define MAX_ROWS 10
#define MAX_TILES_PER_ROW 7
#define WORLD_SIZEX 76
#define WORLD_SIZEY 76
#define NOT_WALKABLE 0
#define WALKABLE 1
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define SCREEN_BPP 16
#define BITMAP_ID 0x4D42
// Macros /////////////////////////////////
// tests if a key is up or down
#define KEYDOWN(name,key) (name[key] & 0x80)
// Classes & Structures ///////////////////
struct MAP_STRUCT {
int tileID;
int WalkAble;
int BuildAble;
int tileposx;
int tileposy;
int Flags;
}map[WORLD_SIZEX][WORLD_SIZEY];
#endif
[/source]
I don''t know, maybe Im doing something wrong
[sourc]
#ifndef GAME_INCLUDES_H
#define GAME_INCLUDES_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "ddutil.h"
// Funtions /////////////////////////////
void CreateMap(void);
void LoadMap(void);
unsigned long GetCpuSpeed();
void GetFrameRate(LPDIRECTDRAWSURFACE7 lpDDSBack, DWORD dwTime);
void DrawTiles(LPDIRECTDRAWSURFACE7 lpDDSBack, LPDIRECTDRAWSURFACE7 tilebmp, HWND main_window_handle, int WorldPosx, int WorldPosy);
int CheckMouse(RECT mouse, RECT button);
void FillMap();
int MouseTileOver(RECT mouse, int tileposx, int tileposy);
// Globals //////////////////////////////
static DWORD dwFrameTime;
static DWORD dwFrames;
static DWORD dwFrameCount;
// Defines //////////////////////////////
#define WINDOW_CLASS_NAME "WINCLASS1"
#define TILE_WIDTH 64
#define TILE_HEIGHT 89
#define MAX_ROWS 10
#define MAX_TILES_PER_ROW 7
#define WORLD_SIZEX 76
#define WORLD_SIZEY 76
#define NOT_WALKABLE 0
#define WALKABLE 1
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define SCREEN_BPP 16
#define BITMAP_ID 0x4D42
// Macros /////////////////////////////////
// tests if a key is up or down
#define KEYDOWN(name,key) (name[key] & 0x80)
// Classes & Structures ///////////////////
struct MAP_STRUCT {
int tileID;
int WalkAble;
int BuildAble;
int tileposx;
int tileposy;
int Flags;
}map[WORLD_SIZEX][WORLD_SIZEY];
#endif
[/source]
I don''t know, maybe Im doing something wrong
This (at the buttom of struct)
}map[WORLD_SIZEX][WORLD_SIZEY];
is just like defining a global variable in your header file, like int x; (won't work if you include in more than one cpp)
Do you only need to have access to this with a few functions? It dosn't look like you're doing any OOP so...
There's a bunch of ways you could get around this (this might not be the best for you), for your header file, how about:
#ifndef H_MAP
#define H_MAP
struct MAP_STRUCT {
int tileID;
int WalkAble;
int BuildAble;
int tileposx;
int tileposy;
int Flags;
};
#endif
Now when you initialize your program in your main or WinMain (don't know what kind of compiler you're using etc), do something like this:
int main(void)
{
MAP_STRUCT Map[x][y];
// do any map init you need to do...
//when a function needs the struct, just pass it like this:
someFunction(Map);
}
Now the trick to passing multidimensional arrays (that's your real problem if you do it this way) is to make the other function like this:
void someFunction(MAP_STRUCT m[][y])
// you need to put the size of the second element of the array (y), cuz the compiler needs it
{
//access your array normally
m[0][0].tileID = 2;
}
If you're doing OOP you can do it differently, etc. But maybe this will get you past the problem, anyway...
What you were essentially doing was declaring a global anyway... and globals stink in general.
===========================================
As far as the laws of mathematics refer to reality, they are not certain, and as far as they are certain, they do not refer to reality.
-Albert Einstein
Edited by - Tebriel on August 7, 2000 1:36:39 AM
}map[WORLD_SIZEX][WORLD_SIZEY];
is just like defining a global variable in your header file, like int x; (won't work if you include in more than one cpp)
Do you only need to have access to this with a few functions? It dosn't look like you're doing any OOP so...
There's a bunch of ways you could get around this (this might not be the best for you), for your header file, how about:
#ifndef H_MAP
#define H_MAP
struct MAP_STRUCT {
int tileID;
int WalkAble;
int BuildAble;
int tileposx;
int tileposy;
int Flags;
};
#endif
Now when you initialize your program in your main or WinMain (don't know what kind of compiler you're using etc), do something like this:
int main(void)
{
MAP_STRUCT Map[x][y];
// do any map init you need to do...
//when a function needs the struct, just pass it like this:
someFunction(Map);
}
Now the trick to passing multidimensional arrays (that's your real problem if you do it this way) is to make the other function like this:
void someFunction(MAP_STRUCT m[][y])
// you need to put the size of the second element of the array (y), cuz the compiler needs it
{
//access your array normally
m[0][0].tileID = 2;
}
If you're doing OOP you can do it differently, etc. But maybe this will get you past the problem, anyway...
What you were essentially doing was declaring a global anyway... and globals stink in general.
===========================================
As far as the laws of mathematics refer to reality, they are not certain, and as far as they are certain, they do not refer to reality.
-Albert Einstein
Edited by - Tebriel on August 7, 2000 1:36:39 AM
ok I''ve got any easier way to fix this... you only need to make 2 small adjustments to your code....
in your header file...
after where you define you struct...
do this:
{source]
extern map_struct map[WIDTH][HEIGHT];
[/source]
this tells the compiler that the real defintion is in another file... but this way it knows that in the end it will be there...
then in ONE of your cpp files(probably the one that does the most with the map=)...
so whats happening is this
in some.cpp it cuts and pasts the code from the header file into it...
so its like this:
int fun;
do stuff...
then in another.cpp is cuts and pasts the code from the header...
so
int fun;
do more stuff...
then it compiles it and everythings good until it trys to link the cpps(now obj) togeather... when it finds that both have
int fun;
define in there code it freaks out....
the same thing happens with your structor... well at least the var of type map_struct...
this isn''t exactly how it happens but its a simple way to think of it happening =)...
Great Milenko
in your header file...
after where you define you struct...
do this:
{source]
extern map_struct map[WIDTH][HEIGHT];
[/source]
this tells the compiler that the real defintion is in another file... but this way it knows that in the end it will be there...
then in ONE of your cpp files(probably the one that does the most with the map=)...
map_struct map[WIDTH][HEIGHT];[/source]this tells the compiler to ignore the extern in the header and make the var for real...and heres a little note on why this is happening...you can think of #include as an auto cut and past... its like making you cpp... then when its time to compile it you open up an other file and copy and past everything in it into your cpp file...so like this[source]some.hint fun;some.cpp#include "some.h"do stuffanother.cpp#include "some.h"do some more stuff
so whats happening is this
in some.cpp it cuts and pasts the code from the header file into it...
so its like this:
int fun;
do stuff...
then in another.cpp is cuts and pasts the code from the header...
so
int fun;
do more stuff...
then it compiles it and everythings good until it trys to link the cpps(now obj) togeather... when it finds that both have
int fun;
define in there code it freaks out....
the same thing happens with your structor... well at least the var of type map_struct...
this isn''t exactly how it happens but its a simple way to think of it happening =)...
Great Milenko
Words Of Wisdom:
"Never Stick A Pretzel In Your Butt It Might Break Off In There."
http://www.crosswinds.net/~milenko
http://www.crosswinds.net/~pirotech
The Great Milenko"Don't stick a pretzel up your ass, it might get stuck in there.""Computer Programming is findding the right wrench to hammer in the correct screw."
Aha! The whole time I KNEW there was an easy way to do it... that''s what I get for posting in the middle of the night! Thanks for pointing that out.
===========================================
As far as the laws of mathematics refer to reality, they are not certain, and as far as they are certain, they do not refer to reality.
-Albert Einstein
===========================================
As far as the laws of mathematics refer to reality, they are not certain, and as far as they are certain, they do not refer to reality.
-Albert Einstein
As mentioned above by others putting the data initialization in an .h means it's initialized multiple times.
the method I use is:
in MyDefs.h:
#ifdef MAIN_PROGRAM_DEFINING_MACRO
#define DEFINE
#else
#define DEFINE extern
#endif
struct MAP_STRUCT {
int tileID;
int WalkAble;
int BuildAble;
int tileposx;
int tileposy;
int Flags;
};
DEFINE map[WORLD_SIZEX][WORLD_SIZEY];
in the main .cpp program:
#define MAIN_PROGRAM_DEFINING_MACRO 1
What happens is that only in the main .cpp program does the
initialization take place and in the others
(sans the MAIN_PROGRAM_DEFINING_MACRO) the map declaration shows up as an extern variable.
Neat little trick I learned from a book by Diane Gruber.
Also I prefer to use an array of pointers so I can have a variable amount of map sizes
ZoomBoy
Developing a 2D RPG with skills, weapons, and adventure.
See my character editor, Tile editor, diary, 3D Art resources at
Check out my web-site
Edited by - ZoomBoy on August 7, 2000 4:19:19 AM
the method I use is:
in MyDefs.h:
#ifdef MAIN_PROGRAM_DEFINING_MACRO
#define DEFINE
#else
#define DEFINE extern
#endif
struct MAP_STRUCT {
int tileID;
int WalkAble;
int BuildAble;
int tileposx;
int tileposy;
int Flags;
};
DEFINE map[WORLD_SIZEX][WORLD_SIZEY];
in the main .cpp program:
#define MAIN_PROGRAM_DEFINING_MACRO 1
What happens is that only in the main .cpp program does the
initialization take place and in the others
(sans the MAIN_PROGRAM_DEFINING_MACRO) the map declaration shows up as an extern variable.
Neat little trick I learned from a book by Diane Gruber.
Also I prefer to use an array of pointers so I can have a variable amount of map sizes
ZoomBoy
Developing a 2D RPG with skills, weapons, and adventure.
See my character editor, Tile editor, diary, 3D Art resources at
Check out my web-site
Edited by - ZoomBoy on August 7, 2000 4:19:19 AM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement