【发布时间】:2010-12-15 05:27:45
【问题描述】:
我知道我在这里遗漏了一些简单的东西,但我有一个我专门研究的类的模板化成员函数。
MyClass
{
template<typename T> T GetTFromVariable(shared_ptr<TOtSimpleVariable> v, string s);
}
template<typename T>
T MyClass::GetTFromVariable(shared_ptr<TOtSimpleVariable> v, string s)
{
throw std::runtime_error("Don't know how to convert " + ToString(v->GetString()));
}
template<>
int MyClass::GetTFromVariable<int>(shared_ptr<TOtSimpleVariable> v, string s)
{
return v->GetInteger();
}
template<>
string MyClass::GetTFromVariable<string>(shared_ptr<TOtSimpleVariable> v, string s)
{
return v->GetString();
}
// etc for other specialisations.
这是在我的头文件中定义的(应该是模板),但是当我去编译时,我得到了一堆多重定义的符号,一个典型的错误是:
OtCustomZenith_logic.lib(PtPathOutput.obj) : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall MyClass::GetTFromVariable<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(class boost::shared_ptr<class TOtSimpleVariable>,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??$GetTFromVariable@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@CommandProperties@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$shared_ptr@VTOtSimpleVariable@@@boost@@V12@@Z) already defined in TableFareSystem_test.obj
我可以通过内联方法来修复它,但我认为这没有必要......我忘记了什么?
编辑:我正在使用 Visual Studio 2010
【问题讨论】:
-
你用的是什么工具链?海合会?视觉的东西?如果是 GCC,构建和链接行是什么样的?