Hello everyone. I've been doing C++ for about a year and ahalf now, but I'm still struggling (such a learning curve!). I managed to narrow down a particularly nasty error message
--
error LNK2001: unresolved external symbol "private: static class Variable * Token::VariableArray" (?VariableArray@Token@@0PAVVariable@@A) MathTree.obj
--
It appears that my VariableArray needs to be initialized... Here is pieces of the code (enough to understand the basic structure. I have a class called Variable (In Variable.h). It holds information on it's Value and if it is defined or not (among other functions, like to set it's value and retrieve it, etc). Now, I made another class called Token (in Token.h), which has this in it's Private section...
--
static Variable VariableArray [NumVariables];
//NumVariables is declared in the public as...
enum Variables { NumVariables = 100 };
--
Now, I have this in my Token.cpp file...
--
Variable Token::VariableArray [NumVariables];
--
Now, I am linking this all to another program. When I do a link (all the proper information is filled out), the error message comes up. If I put all of my files from the c++ program I am trying to link into the one I'm linking it to, the error message goes away. This issue is driving me crazy. I guess I am going to have to just copy over all of these .cpp and .h files over to my new program to bypass this issue for now. Anyone able to see the cause of the error?
Token.cpp includes Token.h, which includes Variable.h... I included Token.h in MathTree.h.
If I do #include <Token.cpp>, I get a lot of "Already defined in Main.obj (I made sure to add #ifndef, #define, #endif on all of my Header and include files to make sure something like this doesn't come up, yet it does??).
It could be an issue of a misused #include in my Token program, since there is no issue when I just simply combine both program together in one solution.
Suppose you have three files
main.cpp
foo.h
foo.cpp
The file "foo.h" is #included by both "main.cpp" and "foo.cpp".
To compile it, you must compile both "main.cpp" and "foo.cpp" and link them into your final executable. You can do it in individual steps:
g++ -c main.cpp
g++ -c foo.cpp
g++ -o myprog main.o foo.o
Or you can do it in one step:
g++ -o myprog main.cpp foo.cpp
Thereafter you have the file "myprog.exe" which you can execute.
I've used the GCC command-line compiler as an option. If you are using an IDE, you must go to the Project menu and Add the "foo.h" and foo.cpp" files as part of your project.