'PolishStack'不是类模板,虚函数重影错误

'PolishStack' is not a class template, virtual function ghost error

本文关键字:函数 错误 重影 PolishStack      更新时间:2023-10-16

我在实现基于抽象父类的类时遇到了一些问题。据说PolishStack是一个抽象类,尽管所有的虚拟函数都是编码的:

In file included from braincalc.cpp:10:
./polstack.h:15:7: error: explicit specialization of non-template class 'PolishStack'
class PolishStack<T> : public AbstractStack<T> {
      ^          ~~~
braincalc.cpp:13:21: error: variable type 'PolishStack<char>' is an abstract class
        PolishStack <char> stk;
                           ^
./abstractstack.h:53:16: note: unimplemented pure virtual method 'isEmpty' in
      'PolishStack'
  virtual bool isEmpty() const = 0;

这是我的课堂标题:

#ifndef POLSTACK_H
#define POLSTACK_H
#include <iostream>
using namespace std;
#include "abstractstack.h"

template <typename T>
class PolishStack<T> : public AbstractStack<T> {
        T* data;
        int mMax;
        int mTop;
        public:
                PolishStack();
                bool isEmpty();
                const T& top() const throw (Oops);
                void push(const T& x);
                void pop();
                void clear();
                //my funcs:
                void printStack();

                ~PolishStack();
};
#endif

我不想因为其他学生作弊而泄露我所有的代码,所以我会发布错误抱怨的函数:

#include "polstack.h"
//...
template <typename T>
bool PolishStack<T>::isEmpty() {
        if(mTop == 0)
                return true;
    return false;
}
//...

正如其他人所说,它应该是:

template<typename T>
class PolishStack : public AbstractStack<T>

/abstractstack.h:53:16:注意:中未实现的纯虚拟方法"isEmpty"PolishStack"
虚拟布尔isEmpty()const=0;

你错过了const:

template<typename T>
bool PolishStack<T>::isEmpty() const
//                             ^^^^^
{
        if(mTop == 0)
                return true;
    return false;
}

注意:当您尝试使用不同的签名覆盖函数时(即,您引入了一个新的函数重载,而不是覆盖virtual),应该使用override关键字来通知您。

template<typename T>
class PolishStack : public AbstractStack<T>
{
public:
    ...
    bool isEmpty() const override;
    ...
};

没有所有的代码很难判断,但我注意到的一件事是:

class PolishStack<T> : public AbstractStack<T> {

应该只是:

class PolishStack : public AbstractStack<T> {

这肯定会修复第一个错误,也可能(但可能不会)修复第二个错误。

尝试更改为

template <typename T>
class PolishStack : public AbstractStack<T>

附带说明:不赞成使用异常说明符throw (Oops)