c++ 将函数的部分模板特殊化为纯虚拟 [重复]

c++ Partial template specialization of function as pure vitrual [duplicate]

提问人:Vahag Chakhoyan 提问时间:11/1/2022 更新时间:11/1/2022 访问量:84

问:

假设我有一个 C++ 模板

template<typename T>
class my_class
{
    virtual void my_func(int i);
};

现在我想部分专业化为一个纯粹的虚拟函数;my_func

template<>
my_class<ExcistingClass>::my_func(int i) = 0;

但是编译器给出错误

error C2988: unrecognizable template declaration/definition
error C2059: syntax error : '='

为什么编译器无法识别我写的逻辑,我怎样才能正确地实现这样的逻辑?

C++ 模板 纯-虚拟 部分-专业化

评论

0赞 463035818_is_not_an_ai 11/1/2022
你不能挑剔。你需要让整个班级都专攻
0赞 Vahag Chakhoyan 11/1/2022
@463035818_is_not_a_number课程的哪一部分不是专业的?
0赞 463035818_is_not_an_ai 11/1/2022
一切都不是专业的。我想你假设,因为在主模板中只有一种方法,所以在专业化中也只有一种方法。但事实并非如此。你需要使整个班级专业化,在专业化中可以有任何东西,0 到你喜欢的成员。
0赞 user12002570 11/1/2022
请参阅如何询问第一步是“搜索然后研究”,您会找到大量相关的 SO 帖子。
0赞 user12002570 11/1/2022
Dupe1、Dupe2、Dupe3 和 Dupe4

答: 暂无答案