模板特化,多个定义符号

新手上路,请多包涵

我知道我在这里遗漏了一些简单的东西,但是我有一个我专门研究的类的模板化成员函数。

 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 许可协议

阅读 455
2 个回答

正如 Alf 所指出的,完全专业化不再是模板。但是,我不确定它 是否 必须内联定义。您还应该能够拆分声明和定义。

即在您的标题中有:

 template<>
int MyClass::GetTFromVariable<int>(shared_ptr<TOtSimpleVariable> v, string s);

并在实施中有:

 template<>
int MyClass::GetTFromVariable<int>(shared_ptr<TOtSimpleVariable> v, string s)
{
    return v->GetInteger();
}

我还曾认为,根据权利,模板化定义也应该显式 _内联_(我一直这样做),但如果给定的编译器在为模板应用 ODR 时松懈,也不会太惊讶。我很想看看另有说明的标准参考。

原文由 Keith 发布,翻译遵循 CC BY-SA 2.5 许可协议

完整的专业化不再是模板。这是一个具体的功能。因此,它需要(隐式或显式)声明 inline 。最简单的方法是在返回类型规范之前添加该关键字。

也就是说,提供的代码与您的错误消息不对应。

您的错误消息涉及返回类型 std::string ,而不是返回类型 int

原文由 Cheers and hth. - Alf 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题