gpt4 book ai didi

C++ 无法覆盖纯虚函数

转载 作者:太空宇宙 更新时间:2023-11-04 11:39:15 27 4
gpt4 key购买 nike

SortedList.h是抽象模板(纯虚函数),LinkedSortedList.h派生SortedList.h是模板,LinkedSortedList.cpp是实现LinkedSortedList.h中函数的模板。

我的错误是当我试图在 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'

error C2838: 'clear' : illegal qualified name in member declaration

我试过:

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

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

这里是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

这里是 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 是 LinkedSortedList 的实现,最后有这两行:

template class LinkedSortedList<int>;
template class LinkedSortedList<double>;

最佳答案

您的覆盖不正确。覆盖是派生类的成员,而不是基类。

此外,您的构造函数没有理由成为函数模板。

我认为你的类应该看起来更像这样:

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;
};

关于C++ 无法覆盖纯虚函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22024020/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com