【问题标题】:Compiler says that defined function is virtual编译器说定义的函数是虚拟的
【发布时间】:2017-10-18 10:01:12
【问题描述】:

当我尝试只使用 stackType 的构造函数时,编译器说我不能,因为重载的 == 是纯虚拟的。但是,如您所见,我在 stackType 中重新定义了它。请帮忙。 (我认为运算符可以声明为纯虚拟,但我不确定。我是 C++ 新手)。

谢谢!

我将代码缩减到最低限度(学校作业):

#include <iostream>
#include <cstdlib>
#include <cassert>

using namespace std;

template <class Type>
class stackADT
{
public:
    virtual bool operator ==(const stackADT<Type> & b) = 0;
};
template <class Type>
class stackType: public stackADT<Type>
{
public:
    bool isFullStack() const;
    stackType(int stackSize = 100);
    bool operator == (const stackType<Type> & b) {
        if (this->stackTop != b.stackTop) {
            return false;
        }
        else {
            bool equivalence = true;
            for (int cntr = 0; cntr < b.stackTop; cntr++) {
                if (this->list[cntr] != b.list[cntr]) {
                    equivalence = false;
                }
            }
            return equivalence;
        }
    }
private:
    int maxStackSize; //variable to store the maximum stack size
    int stackTop;     //variable to point to the top of the stack
    Type *list;       //pointer to the array that holds the
                      //stack elements
};
template <class Type>
bool stackType<Type>::isFullStack() const
{
    return(stackTop == maxStackSize);
} //end isFullStack

template <class Type>
template <class Type>
stackType<Type>::stackType(int stackSize) 
{
    if (stackSize <= 0)
    {
        cout << "Size of the array to hold the stack must "
             << "be positive." << endl;
        cout << "Creating an array of size 100." << endl;

        maxStackSize = 100;
    }
    else
        maxStackSize = stackSize;   //set the stack size to 
                                    //the value specified by
                                    //the parameter stackSize

    stackTop = 0;                   //set stackTop to 0
    list = new Type[maxStackSize];  //create the array to
                                    //hold the stack elements
}//end constructor



int main() {
    stackType<int> a(34);
}

【问题讨论】:

  • edit您的问题提供minimal reproducible example
  • 不要在基类中实现operator=。您允许子类与不同类型的子类进行比较。例如,给定一个Shape 基类,您可以将正方形与圆形进行比较。

标签: c++ operator-overloading pure-virtual


【解决方案1】:

stackADT 中的operator== 采用const stackADT&lt;Type&gt;&amp; 类型的参数,stackType 中的operator== 采用const stackType&lt;Type&gt;&amp; 类型之一。由于它们具有不同的签名,因此它们具有不同的功能。

如果您想确保派生类中的函数覆盖基类中的函数,您可以(在 C++11 中)使用 override 关键字。如果你的函数没有覆盖任何东西,它会让编译器抱怨。

就目前而言,您的抽象基类要求每个派生类都与基类具有可比性。因此,您可以仅基于基础中可用的成员进行比较。由于比较应该是可传递的,这也意味着可以仅基于基类中存在的成员来比较不同的派生类。如果这不是您想要的,您应该从基础中删除运算符。

【讨论】:

  • 我尝试更改它,但这会取消我在内部使用的所有成员变量的资格。此外,当我为该覆盖传递所需的参数时,如何为 == 创建(本质上是)一个新的成员函数或定义使其无效?
  • @RaymondIacobacci 我不确定你在问什么,你能详细说明一下吗?
  • 如果我保持定义原样(派生 == 具有 stackType 的参数),即使我没有重新定义,派生类也不会成为非抽象类我的另一个 ==?
  • 此外,将派生参数更改为 stackADT 将使参数/对象内部的所有成员变量都未定义。
  • @RaymondIacobacci 查看我的答案的编辑。忘记转换。就目前而言,您要求可以仅基于 stackADT 的成员来比较 stackADT 的两个派生类。如果这不是你想要的,你应该从基类中删除它。
【解决方案2】:

这个

bool operator == (const stackType<Type> & b)

不覆盖

virtual bool operator ==(const stackADT<Type> & b) = 0

因为参数类型不同。第一个采用对stackType&lt;Type&gt; 的常量引用。第二个采用 const 引用 stackADT&lt;Type&gt;. 所以你的派生类中有两个不同的 operator== 函数,第一个是纯虚拟的——因此编译器错误。

这里引用了几种解决此问题的方法: Dynamic Casts or Function Overloads? 一种方法涉及使用 dynamic_cast,另一种方法使用 双重调度

【讨论】:

  • 如果您从我的答案(或类似内容)中添加有关覆盖关键字的部分,我将删除我的答案。很高兴您提供了解决方案。
  • 我不能使用 cast 来完成我的作业...还有什么我可以用来定义它的吗?或者它甚至不应该是基类中的虚函数?
  • @RaymondIacobacci 老实说,stackADT 类目前不提供任何功能。你可以删除它。
  • 我必须保留它,因为它是我所需源代码的一部分。相信我,如果我不需要,我会删除它。
  • 抱歉,发现做这种转换毕竟不是一个好主意,所以我把我的答案带回来了......
猜你喜欢
  • 2014-06-24
  • 1970-01-01
  • 2011-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-25
  • 2010-10-27
  • 1970-01-01
相关资源
最近更新 更多