【问题标题】:Object of abstract class type "Rectangle" is not allowed不允许抽象类类型“矩形”的对象
【发布时间】:2013-04-18 01:43:14
【问题描述】:
//QuizShape.h
#ifndef QUIZSHAPE_H
#define QUIZHAPE_H
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

class QuizShape
{
protected:
    //outer and inner symbols, and label
    char border, inner;
    string quizLabel;
public:
    //base class constructor with defaults
    QuizShape(char out = '*', char in = '+', string name = "3x3 Square")
    {
        border = out;
        inner = in;
        quizLabel = name;
        cout << "base class constructor, values set" << endl << endl;
    };

    //getters
    char getBorder() const
    { return border; }
    char getInner() const
    { return inner; }
    string getQuizLabel() const
    { return quizLabel; }

    //virtual functions to be defined later
    virtual void draw( ) = 0;
    virtual int getArea( ) = 0;
    virtual int getPerimeter( ) = 0;

};


class Rectangle : public QuizShape
{
protected:
    //height and with of a rectangle to be drawn
    int height, width;
public:
    //derived class constructor
    Rectangle(char out, char in, string name,
                int h = 3, int w = 3):QuizShape(out, in, name)
    {
        height = h;
        width = w;
        cout << "derived class constructor, values set" << endl << endl;
    }

    //getters
    int getHeight() const
    { return height; }
    int getWidth() const
    { return width; }

    //*********************************************
    virtual void draw(const Rectangle &rect1)
    {
        cout << "draw func" << endl;
        cout << rect1.height << endl;
        cout << rect1.getWidth() << endl;
        cout << rect1.getQuizLabel() << endl;
    }

    virtual int getArea(const Rectangle &rect2)
    {
        cout << "area func" << endl;
        cout << rect2.getInner() << endl;
        cout << rect2.getBorder() << endl;
    }

    virtual int getPerimeter(const Rectangle &rect3)
    {
        cout << "perim func" << endl;
        cout << rect3.height << endl;
        cout << rect3.getWidth() << endl;
        cout << rect3.getQuizLabel() << endl;   
    }
    //************************************************
};



#endif

这些是目前的类类型。

//QuizShape.cpp
#include "QuizShape.h"

目前这只是桥接文件。

//pass7.cpp
#include "QuizShape.cpp"

int main()
{
    Rectangle r1('+', '-', "lol", 4, 5);

    cout << r1.getHeight() << endl;
    cout << r1.getWidth() << endl;
    cout << r1.getInner() << endl;
    cout << r1.getBorder() << endl;
    cout << r1.getQuizLabel() << endl;

    system("pause");
    return 0;
}

由于 Rectangle 应该是一个抽象类,因此代码无法编译,当将鼠标悬停在 main 中的 r1 声明上时,我收到错误

“不允许抽象类类型“矩形”的对象”。

我已经检查了本网站和其他网站上的其他答案,但没有遇到解决问题的方法。

注意:我了解虚函数的语句以 =0; 结尾。使类成为抽象类。 QuizShape 应该是抽象的。我已经在 Rectangle 中定义了虚函数,但它仍然是一个抽象类。

如何修改虚函数 Rectangle 类,让 Rectangle 不再是抽象的?

【问题讨论】:

  • 那些是阴影。我相信对此有警告。
  • 你的getArea,getArea,getPerimeterQuizShape中声明的函数有不同的函数签名

标签: c++ abstract-class virtual-functions


【解决方案1】:

您在抽象类QuizShape 中的方法是:

virtual void draw( ) = 0;
virtual int getArea( ) = 0;
virtual int getPerimeter( ) = 0;

但在Rectangle 中,它们将const Rectangle &amp;rect1 作为参数,因此您可以隐藏方法,而根本不会覆盖抽象方法。 Rectangle 中的方法需要与抽象基类中的方法具有相同的签名。

【讨论】:

  • 当然,如此明显的东西我永远也看不到。谢谢先生。
【解决方案2】:

被覆盖的方法必须具有完全相同的签名,在派生类中您已为它们提供参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多