這讓我在過去的一個半小時內生氣。我知道這是一件小事,但無法找到問題所在(事實上,這是一個下雨的星期五下午,當然,這沒有幫助)。模板方法未定義的參考錯誤
我已經定義了下面的類,將舉行從文件中讀取配置參數,並讓我從我的程序訪問它們:
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);
};
其中方法convert()
在VAConfig.cpp
被定義爲
template <typename T>
T VAConfig::convert(const std::string& value)
{
T t;
std::istringstream iss(value, std::istringstream::in);
iss >> t;
return t;
}
所有很簡單。但是,當我從我的主程序測試使用
int y = parameters->convert<int>("5");
我收到undefined reference to 'int VAConfig::convert<int>...'
編譯錯誤。同上readParameter()
。
考察了很多模板,教程,但不需經過不知道這一點。有任何想法嗎?
一個半小時並沒有那麼糟......它昨天殺了我3。 – 2012-08-16 16:24:09