![]() |
|
#1
|
|||
|
|||
LNK2005 Error: Already defined in...Hello again! Before I begin with my problem I want to thank you guys again for helping me last time. I wouldn't have gotten anywhere.
Well basically I have my main file, we'll call main.cpp, which includes a class called game.cpp. game.cpp includes a header file called jolt.h. When I try to compile I get a LNK2005 error that tells me that everything in jolt.h is already defined in main.obj The error looks like this: game.obj : error LNK2005: "bool __cdecl joltLoadTexture(char *,unsigned int &,int)" (?joltLoadTexture@@YA_NPADAAIH@Z) already defined in main.obj and there are 19 of these, each refering to a different function or variable. Of course I did some research before posting and most of what help there is assumes two things: 1) I am an expert with C++, and 2) That I am using a library, not a header file. I am using Microsoft Visual C++ 6.0, in a Win32 Application project. Thanks for all your help!!!! -Jon |
|||
|
#2
|
||||
|
||||
|
Hello TekiFreek.
Did you include jolt.h in game.cpp & main.cpp? If so, did you use a #ifndef statement to assure that these things were only included once? |
|
#3
|
|||
|
|||
|
I included jolt.h in game.cpp only, and I also used #ifndef.
|
|
#4
|
||||
|
||||
|
Quote:
You say that you are not using a library. Where is joltLoadTexture implemented? Under game.cpp? This is a pretty difficult error to trace with the source code, without the source code, I am just grasping at straws. Sorry I couldn't be of more help. Perhaps, someone else has some ideas???? |
|
#5
|
|||
|
|||
|
I think I might see a problem... the header file is not a class, but a slew of functions that I normally included at the top of my main.cpp. Putting these various functions above a game class doesn't seem to make much sense because the class wouldn't be able to use them... So this puts me in another quandary.
For one, you can't define a function above a class and then use it within the class, can you? And if you can't, which I am assuming is the case, why is it that I can include the OpenGL .h files and be able to use the functions within them when defining a class? Does it have something to do with the fact that they have libraries associated with them? And if that is the case, how do I create my own libraries so that my classes can have access to functions within my jolt header file (which contains several functions that make loading textures and stuff very easy)? Thanks! |
|
#6
|
||||
|
||||
|
I think you should post some of your code regarding the class and function declaration and the file related. That would be rather helpful to provide some advice. thankx.
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds |
|
#7
|
||||
|
||||
|
Quote:
Most header files only contain the definitions of the functions and not the implementation. The implementation is generally a library linked into the final program by the linker. If you look through a typical header file, you generally will not find any implementation, only definitions and function declarations. You should be able to make your own libraries fairly easily. You need to make a header file which has all of the declarations. I don't have Visual C++ in front of me, but when you start a project you should be able to tell it whether you want to make a program or a library. Either static(lib) or dynamic(dll). Then when you make a program that needs this library, tell the linker to link it to your program. I do have a copy of Visual C++, but I don't boot into Windows too often as of late, so if you are struggling let me know and I can get you better instructions. (or someone else may be able to give you better guidance) |
|
#8
|
|||
|
|||
|
Ok cool. I've created a jolt library (miracle?) and I've gotten the errors down to 13, which consist only of variable declarations now... no longer function definitions. I think this is due to the fact that I left all the variable declarations in the .h file. I'll do a bit of research and see if I can correct this problem.
Thanks! |
|
#9
|
|||
|
|||
|
Okay here I pose a new question. When I stick everything in the library file, the "already declared" error goes away, however, the variables that I defined globally in the library file are inaccessible for functions in my class. Somebody earlier wanted me to post up code to make things easier to understand so here it goes:
CPP / C++ / C Code:
Hope that helps! |
|
#10
|
||||
|
||||
|
Ok. Here is what I suggest, forward declare your global in the headear file.
CPP / C++ / C Code:
And the define them in the appropriate cpp file, that is in your case in the game.cpp file. CPP / C++ / C Code:
Now you should be able to access them all. You asked why you cannot access those global var's?? Its rather simple. globals scope of declararion is from the point where its declared to the end of the file.You declared the globals in jolt.h and trying to access it from game.cpp which is not possible. To access it,from another file, you need to use the above code. In the above code, the globals is forward declared in a header file. You have to have a source file(game.cpp) to define them in and in order to access them from the source file you have to set them as extern in the header file to let the compiler know that they are declared elsewhere. __________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds |
Recent GIDBlog
Toyota - 2009 May Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The