[英]C++ Failing to override Pure Virtual Functions
SortedList.h is an abstract template (pure virtual functions), LinkedSortedList.h derives SortedList.h and is a template, and LinkedSortedList.cpp is a template implementing the functions in LinkedSortedList.h. SortedList.h是一个抽象模板(纯虚函数),LinkedSortedList.h派生SortedList.h并是一个模板,LinkedSortedList.cpp是实现LinkedSortedList.h中的功能的模板。
My Error is when I'm trying to override the functions from SortedList.h in LinkedSortedList.h. 我的错误是当我尝试覆盖LinkedSortedList.h中SortedList.h中的函数时。
Method in SortedList.h: SortedList.h中的方法:
virtual void clear() = 0;
Method in LinkedSortedList.h: LinkedSortedList.h中的方法:
template <typename Elm> void SortedList<Elm>::clear() override;
error: 错误:
error C3240: 'clear' : must be a non-overloaded abstract member function of 'SortedList'
错误C3240:“清除”:必须是“ SortedList”的未重载抽象成员函数
error C2838: 'clear' : illegal qualified name in member declaration
错误C2838:“清除”:成员声明中的非法限定名称
I've tried: 我试过了:
void SortedList<Elm>::clear(){}
But I still get the same error. 但是我仍然遇到同样的错误。 I've tried to find the solution on the web, but have failed.
我试图在网络上找到解决方案,但是失败了。
Here is SortedList.h It must not be changed: 这是SortedList.h,不得更改:
#ifndef _SortedListClass_
#define _SortedListClass_
template <class Elm> class SortedList {
public:
// -------------------------------------------------------------------
// Pure virtual functions -- you must implement each of the following
// functions in your implementation:
// -------------------------------------------------------------------
// Clear the list. Free any dynamic storage.
virtual void clear() = 0;
// Insert a value into the list. Return true if successful, false
// if failure.
virtual bool insert(Elm newvalue) = 0;
// Get AND DELETE the first element of the list, placing it into the
// return variable "value". If the list is empty, return false, otherwise
// return true.
virtual bool getfirst(Elm &returnvalue) = 0;
// Print out the entire list to cout. Print an appropriate message
// if the list is empty. Note: the "const" keyword indicates that
// this function cannot change the contents of the list.
virtual void print() const = 0;
// Check to see if "value" is in the list. If it is found in the list,
// return true, otherwise return false. Like print(), this function is
// declared with the "const" keyword, and so cannot change the contents
// of the list.
virtual bool find(Elm searchvalue) const = 0;
// Return the number of items in the list
virtual int size() const = 0;
};
#endif
Here is LinkedSortedList.h This can be rewritten if needed: 这是LinkedSortedList.h可以根据需要重写:
#ifndef _LinkedSortedList_
#define _LinkedSortedList_
#include "SortedList.h"
#include "LinkedNode.h"
#include <iostream>
using namespace std;
template <class Elm> class LinkedSortedList:public SortedList<Elm>
{
public:
template <typename Elm> LinkedSortedList();
virtual void SortedList<Elm>::clear();
virtual bool SortedList<Elm>::insert(Elm newvalue);
virtual bool SortedList<Elm>::getfirst(Elm &returnvalue);
virtual void SortedList<Elm>::print() const;
virtual bool SortedList<Elm>::find(Elm searchvalue) const;
virtual int SortedList<Elm>::size() const;
private:
LinkedNode<Elm> *head;
int num;
};
#endif
LinkedSortedList.cpp is the implementation for LinkedSortedList, and has these two lines at the end: LinkedSortedList.cpp是LinkedSortedList的实现,并在末尾有这两行:
template class LinkedSortedList<int>;
template class LinkedSortedList<double>;
You're overriding incorrectly. 您没有正确覆盖。 The overrides are members of your derived class, not the base class.
重写是派生类的成员,而不是基类的成员。
Also, there's no reason for your constructor to be a function template. 同样,您的构造函数没有理由成为函数模板。
I think your class should look more like this: 我认为您的课程应该更像这样:
template <class Elm> class LinkedSortedList : public SortedList<Elm>
{
public:
LinkedSortedList();
void clear() override;
bool insert(Elm newvalue) override;
bool getfirst(Elm &returnvalue) override;
void print() const override;
bool find(Elm searchvalue) const override;
int size() const override;
private:
LinkedNode<Elm> *head;
int num;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.