-
March 11th, 2010, 06:04 PM
#1
Error Linking
Hi,
I'm receiving a linking error, which says my symbols are multiply defined.
These symbols are defined in "one.hpp"
My "main.cpp" includes "two.h", and in "two.h" I have included "one.hpp".
The functions defined in "one.hpp" are used in both "main.cpp" and "two.cpp"
What am I getting these errors? Also all my .h files are wrapped with #ifndef etc.
-
March 11th, 2010, 06:17 PM
#2
Re: Error Linking
Are all #ifndef's properly made? If you're using Visual Studio you can try putting a #pragma once at first line in all headers. If this helps something is wrong with the #ifndef's
-
March 11th, 2010, 06:48 PM
#3
Re: Error Linking
Neither #pragma once nor include guards (#ifndefs) can protect you against linker errors! They're only useful during the compile, not the link.
In general, non-template functions and variables cannot be defined in a header file or else you're likely to end up with this error. You should put them in a cpp file, and if necessary in the case of global variables, extern-declare them in the header.
There are a few other options as well. Declaring functions inline usually allows them to be placed in the header. Declaring variables static works the same way (but may be misleading, because each translation unit will have a different version of the "global" variable!). Singleton classes, classes with static members, etc are also options.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|