Undefined reference error for template method

It drove me crazy in the last hour and a half. I know this little thing, but I can’t find what is wrong (the fact that it does not help on a rainy day on Friday, of course).

I defined the following class, which will contain configuration parameters read from a file, and will give me access to them from my program:

class VAConfig { friend std::ostream& operator<<( std::ostream& lhs, const VAConfig& rhs); private: VAConfig(); static std::string configFilename; static VAConfig* pConfigInstance; static TiXmlDocument* pXmlDoc; std::map<std::string, std::string> valueHash; public: static VAConfig* getInstance(); static void setConfigFileName( std::string& filename ) { configFilename = filename; } virtual ~VAConfig(); void readParameterSet( std::string parameterGroupName ); template<typename T> T readParameter( const std::string parameterName ); template<typename T> T convert( const std::string& value ); }; 

where the convert() method is defined in VAConfig.cpp as

 template <typename T> T VAConfig::convert( const std::string& value ) { T t; std::istringstream iss( value, std::istringstream::in ); iss >> t; return t; } 

Everything is pretty simple. But when I test my main program using

 int y = parameters->convert<int>("5"); 

I get a compilation error undefined reference to 'int VAConfig::convert<int>...' . Same for readParameter() .

Looked at a lot of patterns, but coul didn't get it. Any ideas?

+36
c ++ compilation linker templates
Jul 10 '09 at 19:13
source share
3 answers

Implementation of the embedded code should never be in the .cpp file: your compiler should see them at the same time that it sees the code that calls them (if you are not using an explicit instance to generate the template object code, but even then .cpp using the wrong type file).

What you need to do is move the implementation to a header file or to a file, for example VAConfig.t.hpp , and then #include "VAConfig.t.hpp" whenever you use any template member functions.

+55
Jul 10 '09 at 19:19
source share

If you move the implementation of the template methods (convert and readParameter) to the header file, it should work.

The compiler must have access to implementations of template functions at the points where they were created.

+9
Jul 10 '09 at 19:17
source share

A template method is just ... a template for a method. The template arguments must be populated when the method is created.

It should be possible to create a compiler that contains the declaration of the template method, as well as the step “compile the template”, to compile all the necessary instances of the template method.

This does not apply to microsoft vc. I heard colleagues mumble about this in the case of Unix.

Most compilers create an instance of the template method on demand, where they are used in the source code. To create an instance of a method, the compiler must “see” the body of the template function. That is why the body is most often placed either in the header file, or, for example, in the .h.cpp file, which is then included as the last line of the .h file.

+5
Jul 10 '09 at 19:33
source share



All Articles