# Templatized method in a base class not linking. solved

## Recommended Posts

I've got a base class Base, and an inherited class InheritA. Inside of InheritA I had a templatized function that was working fine. I needed to make an InheritB that would also need this templatized method, so I copied the method from InheritA into Base. Now I get linker errors in InheritA that say it can't find the method in Base. I've tried the method in public and protected sections of Base, Base is publicly inherited. Header code below. strToVal is the templatized method in question.
class XMLLoader
{
public:

...

protected:
template <typename T>
T strToVal(std::string) const;

...
};


class MapLoader : public XMLLoader
{
public:

bool populate();

..

private:
/*
template <typename T>
T strToVal(std::string) const;
*/
};


So XMLLoader is the base class, MapLoader is the inherited. In MapLoader you see the commented out strToVal which is where it used to be when it was working. With the method inside of XMLLoader I get linker errors when I call the function in MapLoader::Populate. So it looks like I'm missing something that makes the compiler generate the necessary versions of the method (bool, float, int). Thanks [Edited by - c0uchm0nster on December 31, 2007 3:56:10 PM]

##### Share on other sites
Unless your compiler supports the export keyword, and I'm willing to bet that yours doesn't, then you can't put the definition of a template in a separate source file without explicit instantiation for specific types. Without explicit instantiation, the complete definition of the template needs to be available at point of instantiation, which means, in effect, that the definition needs to go into the header. (Or an inline file of some sort, etc.)

##### Share on other sites
thanks

cant believe i forgot the delcaration needs to be in the same file.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628332
• Total Posts
2982120

• 22
• 9
• 9
• 13
• 11