LNK2001: unresolved external symbol

Hi

Could someone kindly explain the nature behind a LNK2001 error?

I'm using source files I've previously written in a new project but upon compile I get the following:

1>ParkMiller.obj : error LNK2001: unresolved external symbol "protected: static bool RandomBase::BoostGenerator" (?BoostGenerator@RandomBase@@1_NA)
1>Random3.obj : error LNK2001: unresolved external symbol "protected: static bool RandomBase::BoostGenerator" (?BoostGenerator@RandomBase@@1_NA)


By way of background, BoostGenerator is a bool variable to indicate whether the boost library is required. In this case it's not required but I've included the boost directories in the project anyway (although this didn't fix the error).

Various other Google searches indicate the first check of whether the library has been linked. I took this to mean whether I've included the libraries as above (please correct me if I'm wrong). I'm suspecting it may have something to do with BoostGenerator being a static variable but don't know anything past this.

Please let me know of any other information needed.

Many thanks
The error means your code accesses the variable RandomBase::BoostGenerator, but the linker can't find where the variable was defined.

Make sure that somewhere in your source files you have the line (at file scope):

bool RandomBase::BoostGenerator = false ;

I'm assuming it should be false since you say "in this case it's not required."
Thanks

So in RandomBase I have:
1
2
3
4
5
6
7
class RandomBase
{
// ... //
protected:
	static bool BoostGenerator;
// ... //
};


At the file level, BoostGenerator is set to false in the constructor definition of RandomParkMiller (a child of RandomBase) is written as:
1
2
3
4
5
RandomParkMiller::RandomParkMiller(
// ... //
{
	BoostGenerator = false;
}


This worked before. I'm still rather fuzzy as to why the linker doesn't recognise that the variable has been set?

Edit: Just to add another observation, if BoostGenerator was declared without 'static', the LNK2001 error goes away. However as BoostGenerator is suppose to serve as a 'state variable', this isn't desirable.

Edit: Ah sorry, the lightbulb finally switched on. Because a static variable is actually an external declaration, you need a separate definition outside of RandomBase made in 'main.cpp' (ie. cire's suggestion above). And in the interests of spreading the word:
http://www.daniweb.com/software-development/cpp/threads/213588/error-lnk2001-unresolved-external-symbol-only-happens-with-static-variable
Last edited on
Topic archived. No new replies allowed.