Sign in to follow this  
c0uchm0nster

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:
  XMLLoader();
  XMLLoader(std::string path);
  virtual ~XMLLoader();
  
...

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

...
};


class MapLoader : public XMLLoader
{
public:
  MapLoader(Map&, std::string path);
  MapLoader(Map& map) :
    XMLLoader() {}

  ~MapLoader();

  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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this