不允许抽象类类型 "Rectangle" 的对象

Object of abstract class type "Rectangle" is not allowed

本文关键字:对象 Rectangle 不允许 类型 抽象类      更新时间:2023-10-16
//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;
}

代码将无法编译,因为矩形被认为是一个抽象类,当鼠标悬停在main中的r1声明上时,我收到错误

"不允许抽象类类型为"Rectangle"的对象".

我已经在这个网站和其他网站上检查了其他答案,但没有遇到解决问题的东西。

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

我如何修改虚拟函数矩形类,使矩形不再是抽象的?

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

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

但在Rectangle中,他们将const Rectangle &rect1作为参数,因此您遮蔽了方法而根本不重写抽象方法。您需要在Rectangle中的方法具有与抽象基类中的方法相同的签名。

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