Friday 18 January 2019

c++ - Undefined reference error for template method

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 valueHash;
public:
static VAConfig* getInstance();
static void setConfigFileName( std::string& filename ) { configFilename = filename; }
virtual ~VAConfig();
void readParameterSet( std::string parameterGroupName );
template T readParameter( const std::string parameterName );
template T convert( const std::string& value );
};

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


template 
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("5");

I get an undefined reference to 'int VAConfig::convert...' compilation error. Ditto for readParameter().


Looked at a lot of template tutorials but coul not figure this out. Any ideas?

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...