我知道我在这里遗漏了一些简单的东西,但是我有一个我专门研究的类的模板化成员函数。
class 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
原文由 Jamie Cook 发布,翻译遵循 CC BY-SA 4.0 许可协议
正如 Alf 所指出的,完全专业化不再是模板。但是,我不确定它 是否 必须内联定义。您还应该能够拆分声明和定义。
即在您的标题中有:
并在实施中有:
我还曾认为,根据权利,模板化定义也应该显式 _内联_(我一直这样做),但如果给定的编译器在为模板应用 ODR 时松懈,也不会太惊讶。我很想看看另有说明的标准参考。