Initializing an Array of a Class?

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?
It's probably a stupid question, but is Token.cpp included in the compilation?
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.
What I mean is, are you including (not #including) Token.cpp in the project? Is the compiler actually compiling Token.cpp?
Btw (I dunno if it applies here), it's usually, if not always, not good to #include a .cpp.
Aye, I found that out awhile ago, wmheric x_x.

Token.cpp is included in the Token.h solution, but is not included in the MathTree.h solution. Think that could be the problem?
Sorry, I have no idea what you're saying. Could you please take a screenshot of your project structure and upload it somewhere?
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.

Hope this helps.
Topic archived. No new replies allowed.