I'm sitting on this for quite some time now. I just can't find an answer so it would be cool if you guys could help.
There are two classes involved, one (XMLParser
) is reading from a ifstream and is deserializing objects and stores them in a local veriable. A second is holding a vector<Someclass *> objVec;
, so a vector which holds pointer to objects of a specific type. I'm pulling it all together like this:
ifstream file(filename, ifstream::in);
if (file.is_open())
{
XMLParser xmlParser;
file >> xmlParser;
xmlParser.fill(&objVec);
file.close();
}
XMLParser.h:
class XMLParser
{
private:
vector<Someclass> someVec;
vector<SomeOtherclass> someOtherVec;
public:
template<class T> void fill(std::vector<T*> *vec);
friend std::istream &operator>>(std::istream &stream, XMLParser &ob);
};
std::istream& operator>>(std::istream &stream, XMLParser &ob);
XMLParser.cpp:
std::istream& operator>>(std::istream &stream, XMLParser &ob)
{
//deserialize objects from stream and store them locally
return stream;
}
template <class T> void XMLParser::fill(std::vector<T*> *vec)
{
//fill the vector with the right objects
}
Everything is compiling well, no errors or warnings. But when its time for linking I'm getting a undefined reference to 'void XMLParser::fill<Someclass>(std::vector<SomeClass*, std::allocator<Someclass*> >*)'
, XMLParser.o is of course provided.
what I've tried, among other things:
including the allocator: template<class T> void fill(std::vector<T*, std::allocator<T*> > *vec)
-> same error
writing a specialization for fill width Someclass
: template <> void XMLParser::fill(std::vector<Someclass*> *vec)
-> working! but not what I want
I've searched for some examples on the web and none of them is providing the allocator class for the template, so this doesn"t seams to be the way. The specialization is working, but does anyone know why I need to provide a specialization for every class that might come along? Is there a way around? Cause it doesn't seams to be very generic if I have to provide a specialization for every class that might be in the XML.
Thanks Martin