Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / "already defined in" "error LNK2005"

Author
Message
Fatal Berserker
13
Years of Service
User Offline
Joined: 2nd Jul 2010
Location:
Posted: 10th Oct 2010 16:00
Ive looked this up on msdn and it didnt help too much, http://msdn.microsoft.com/en-us/library/72zdcz6f(VS.80).aspx.

The error is:


The offending code in AI.h (has #pragma once)



I tried changing the declaration to

However, even though that removes errors, it runs the game fine, but even when im trying to set variables i can see in the debugger that it cant actually find the pointer.

I also tried using structs instead, however it gives the exact same error, and exact same result when i make it static.

Is there any way i can fix this?

Thanks in advance.

(If i remove the include to AI.h in Main.cpp it works, however i need Main.cpp aswell as AI.cpp to be able to access these classes.)

Smoke me a kipper, ill be back for breakfast.
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 10th Oct 2010 17:02 Edited at: 10th Oct 2010 17:05
If you write 'int player = 5' in main.cpp, the 'player' variable will be stored in main.obj(compiled .cpp). If you write 'int player = 5' in main.h and include it in main.cpp, it will be stored in main.obj. If you also include main.h from somethingElse.cpp, then 'player' will be stored in both main.obj and somethingElse.obj, the linker will load up all these objects and see you have the same variable stored twice and throw this error.

The reason for this is because .h files aren't special files; during compile, every time you write #include "whatever.h", whatever.h will effectively be pasted into your source file until there's nothing left to include, and that massive source file(translation unit) is compiled into an object. This is why your #pragma once doesn't fix this, because that only prevents the same source being included multiple times per translation unit, as each object is separate(until linking).

Fatal Berserker
13
Years of Service
User Offline
Joined: 2nd Jul 2010
Location:
Posted: 10th Oct 2010 17:51 Edited at: 10th Oct 2010 17:52
ok, i think i understand it, but i dont know how to fix it still, any ideas?

And it only happens with arrays.

Smoke me a kipper, ill be back for breakfast.
Mireben
15
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 10th Oct 2010 20:17 Edited at: 10th Oct 2010 20:22
I think the main question is why do you want to create those arrays in the header file at all? Usually a class design does not need to know anything about its instantiation, meaning that AI.cpp shouldn't need to access the arrays. You can declare the classes in AI.h, write the class functions in AI.cpp and then create and use the arrays in Main.cpp. Think about whether you can change your program design.

If that is not to your liking for some reason, as a workaround you could make a container class which holds the two arrays, then define an "extern" pointer to the container class in the header and instantiate ("new") it in the beginning of the AI.cpp or even Main.cpp. Then it could be accessed from both modules, it's not beatiful but it should work.

EDIT: If the arrays are made "static" in the container class, you don't even need to "new" it. Example:



It's of course a bit uncomfortable to use the container class name always in front of the array name.
Fatal Berserker
13
Years of Service
User Offline
Joined: 2nd Jul 2010
Location:
Posted: 10th Oct 2010 21:50 Edited at: 10th Oct 2010 22:11


I like that idea, thanks, really i should have thought about that earlier.

This site really needs a karma thing or something .

Smoke me a kipper, ill be back for breakfast.
Mireben
15
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 11th Oct 2010 22:49
I'm glad you like it, but I have to add something because the simpler solution somehow didn't occur to me yesterday. This will work as well:



Then in Main.cpp you can use the array without the additional syntax of a container class.

The container class can have its advantages though. If you have functions that handle the array, they can be grouped into the class.
Fatal Berserker
13
Years of Service
User Offline
Joined: 2nd Jul 2010
Location:
Posted: 12th Oct 2010 16:37
Oh, thanks, ill look into that, debugging is ... buggy, when the class method.

Smoke me a kipper, ill be back for breakfast.
Fatal Berserker
13
Years of Service
User Offline
Joined: 2nd Jul 2010
Location:
Posted: 17th Oct 2010 02:59
mm... i think i did something wrong.

I havnt been able to properly look at this for a bit but i got a bug where it only seemed to use 1 array node in my struct...

Basically, when i change 1 value it changes all the values for that variable eg.

lol[0].hi = 0;
lol[1].hi = 1;

it will make lol[0].hi equal to 1.
It does this with both extern and

Its really annoying and i still havnt figured out why it is doing it.

My code for when im using extern (i want to get extern to work more as it isnt buggy in the debugger).

in my .h


Then in my .cpp



Any ideas?

Smoke me a kipper, ill be back for breakfast.
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 17th Oct 2010 05:09
Read a C++ tutorial on what the 'static' keyword does, as you seem to be using it everywhere for no reason.

Mireben
15
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 17th Oct 2010 17:30 Edited at: 17th Oct 2010 17:50
Exactly as Dark Coder says. Static means that there is ONE copy of that variable for all instances of the class (it's a class variable, not an object variable). If you use the static keyword for all variables, then all values in all objects will be the same, because they all change the same one variable.

In the classes from which you create several instances (Units, AIs, Trees, Grass), remove the static keywords. In the container class it's needed because we want to access the arrays without instantiating the class, but in all other places it shouldn't be needed.
Fatal Berserker
13
Years of Service
User Offline
Joined: 2nd Jul 2010
Location:
Posted: 17th Oct 2010 23:10
Oh i see, i thought i had already tried that, thanks

Smoke me a kipper, ill be back for breakfast.

Login to post a reply

Server time is: 2024-06-30 10:59:23
Your offset time is: 2024-06-30 10:59:23