I am using VC++ 2005.
I have a class template located in the file "SchemaParser.h" which defines a static member:
template <typename _alloc>
class SchemaParser
{
protected:
static SaxBaseNoTemp* rootNode;
};
Then I define the static variable in "SchemaParser.cpp":
#include "SchemaParser.h"
template<typename _alloc> smiXML::SaxBaseNoTemp* smiXML::SchemaParser<_alloc>::rootNode = 0;
But when I include and use SchemaParser in main.cpp, I get the following message:
error LNK2001: unresolved external symbol "protected: static class smiXML::SaxBaseNoTemp * smiXML::SchemaParser<class DAll>::rootNode"
I suspect it has something to do with the compiler having to make copies of the template, but I haven't found anything on Google to confirm this. I've only found the suggestion that I define a static in the header's corresponding source file.
If I move the definition of rootNode to the bottom of SchemaParser.h, it works fine. Does anyone know why this is happening? And why is it giving me a linker error when I define rootNode in SchemaParser.cpp?
EDIT:
To clarify, I am placing the same line
template<typename _alloc> smiXML::SaxBaseNoTemp* smiXML::SchemaParser<_alloc>::rootNode = 0;
in two different places, and only one (in SchemaParser.cpp) gives me a linker error. What has me perplexed is that only one of them works.
[Edited by - Guru2012 on May 20, 2006 3:03:09 AM]