You should probably read the article regardless, as I believe you'd not only find the answer to your current question, but probably your second, and third, etc that you are likely to run into later.
If you want the short answer, it's because of multiple inclusion.
In one cpp file define the variable, in all other translation units that need the variable declare as extern.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
In one cpp file define the variable, in all other translation units that need the variable declare as extern.
Russco is right. The error is not because of multiple inclusion but cause the global variable was DEFINED in the header so that each cpp that includes the header would create a new instance of that variable (symbol). The linker then will complain because of that.
So, in the header you would need only a DECLARATION what is made by the extern keyword. And one and only cpp MUST have a definition of the variable simply like
Thanx for the solution itsmeandnobodyelse, I've been trying to understand the concept of classes public and private over and over. Reading many many tutorials about it and it just seems to slip my mental grasp but I'll keep trying to learn and understand it. However, I'm having the same error. I declared it in the first .cpp like you said and extern in the header and the same error exists. What gives?
Last edited by dellthinker; November 11th, 2010 at 10:54 AM.
Bookmarks