This has been driving me mad for the past hour and a half. I know it's a small thing but cannot find what's wrong (the fact that it's a rainy Friday afternoon, of course, does not help).
I have defined the following class that will hold configuration parameters read from a file and will let me access 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 method convert()
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;
}
All quite simple. But when I test from my main program using
int y = parameters->convert<int>("5");
I get an undefined reference to 'int VAConfig::convert<int>...'
compilation error. Ditto for readParameter()
.
Looked at a lot of template tutorials but coul not figure this out. Any ideas?
Templated code implementation should never be in a .cpp
file: your compiler has to see them at the same time as it sees the code that calls them (unless you use explicit instantiation to generate the templated object code, but even then .cpp
is the wrong file type to use).
What you need to do is move the implementation to either the header file, or to a file such as VAConfig.t.hpp
, and then #include "VAConfig.t.hpp"
whenever you use any templated member functions.