C++ 实现从抽象模板派生的模板

标签 c++ templates virtual abstract derived

SortedList.h 是一个抽象模板,LinkedSortedList.h 派生于 SortedList.h 并且是一个模板,而 LinkedSortedList.cpp 是一个实现 LinkedSortedList.h 中的函数的模板,这是我遇到问题的地方。

新的新错误:我认为我现在的问题是我没有正确覆盖 SortedList.h 中的方法。

SortedList.h 中的方法:

virtual void clear() = 0;   

LinkedSortedList.h中的方法:

template <typename Elm> void SortedList<Elm>::clear() override; 

错误:

error C3240: 'clear' : must be a non-overloaded abstract member function of 'SortedList'

我试过:

void SortedList<Elm>::clear(){}

但我仍然得到同样的错误。我试图找到解决方案,但失败了。

最佳答案

您需要告诉编译器您正在定义模板的成员:

template <typename Elm>
LinkedSortedList<Elm>::LinkedSortedList() {

编辑:这更像是多个问题,但不同的错误消息:

c:\users\deltac\documents\visual studio 2010\projects\linked list\linked list\linkedsortedlist.h(23): error C2365: 'LinkedSortedList::size' : redefinition; previous definition was 'member function'

来自代码:

int size() const;
int size;           // Compiler report here?

您不能使用相同的标识符来引用成员变量和成员函数。您可以为不同的成员函数重用相同的标识符(可以重载函数)。将成员重命名为其他名称:例如 int m_size;

错误 2 不是错误,而是日志中上一条错误消息的延续(以see 开头的行不表示新错误。

c:\users\deltac\documents\visual studio 2010\projects\linked list\linked list\main.cpp(27): error C2259: 'LinkedSortedList' : cannot instantiate abstract class

基类中至少有一个纯虚函数(完整的错误消息可能包括它们的列表)没有被覆盖。这使得派生类型成为抽象类型,您无法实例化它。提供适当的定义。

关于C++ 实现从抽象模板派生的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21978515/

相关文章:

c++ - reinterpret_cast 错误还是 UB?

c++ - 定义嵌套模板的合法语法是什么?

c# - 使用虚拟方法是否只有单实例生成的优势?

c++ - 为什么 std::make_unique 而不是 std::unique_ptr::make?

c++ - 将一个用户的 GNU makefile 转换为另一个用户

c++ - BST 以 2d 数组作为节点结构中的键而不是 int

c++ - 使用 std::enable_if<> 的模板特化

数字类型的 C++ 模板

C++ 虚函数意外行为

c# - 如何在 C# 中释放桌面应用程序的虚拟机/专用字节