【问题标题】:define function templated class outside of template [duplicate]在模板之外定义函数模板类[重复]
【发布时间】:2023-03-13 21:01:02
【问题描述】:

这有点难以解释,但基本上,我想做这样的事情:

template <typename T>
class some{
    int hello();
};

//The method needs to be defined outside of the template class declaration and be able to use T defined in the template
int some::hello(){
    T* other = new T;
    T->doSomething();
}

我该怎么做?

【问题讨论】:

  • 对不起,我在任何地方都没有看到问题。
  • 抱歉,已编辑。
  • 你应该使用template &lt;typename T&gt; int some&lt;T&gt;::hello(){ ...
  • @πάνταῥεῖ 你知道这是否有骗局吗?感觉应该有,但是我只能找到成员模板的外行定义。
  • @πάνταῥεῖ 啊,完美,谢谢:)

标签: c++ templates syntax


【解决方案1】:

给你:

template <typename T>
class some{
    int hello();
};

//The method needs to be defined outside of the template class declaration and be able to use T defined in the template
template<typename T>
int some<T>::hello(){
    T* other = new T;
    other->doSomething();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-22
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多