Reputation: 13
I'M parsing an XML file in C++ using RapidXML. I've made treeNode.h and treeNode.cpp files. I'm making a treeNode object to help me with the parsing. When I try to do this though, it's giving me an undefined reference error. The line of code which is giving me the error (I think) is the following line:
treeNode<xml_node<> > * tn = new treeNode<xml_node<> >(root_node);
If you're wondering where I got root_node from, it's defined in my main.cpp as the following:
xml_document<> doc;
doc.parse<0>(&buffer[0]);
xml_node<> * root_node = doc.first_node();
The error message is this:
undefined reference to `treeNode<rapidxml::xml_node<char> >::treeNode(rapidxml::xml_node<char>*)'
collect2: ld returned 1 exit status
These are my three constructors in treeNode.h:
treeNode(T* element1);
treeNode(T* element1, T* parent1);
treeNode(T* element1, T* parent1, vector<treeNode<T>*> child1);
Any help with fixing this error would be much appreciated. Thank you!
Upvotes: 0
Views: 606
Reputation: 66449
I suspect you have implemented the treeNode
methods in "treeNode.cpp".
In that case, they haven't been instantiated.
All your treeNode
implementation needs to be in the treeNode.h
header, so the compiler can instantiate the functions when they are used.
Otherwise, you will only have declarations, no definitions - because the compiler couldn't generate any - and you will get the same linking errors as if you didn't have any implementations at all.
Upvotes: 1