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/