c++ 编译器错误 C2259

这是cpp文件
#include "drawable.h"
#include 
#include 

using namespace std;
#define M_PI 3.14159265358979323846

// Rectangle implementation
Rectangle::Rectangle(double l, double w) : length(l), width(w) {}

double Rectangle::getArea() const {
    return length * width;
}

void Rectangle::draw() const {
    cout << "Drawing a rectangle " << endl;
    for (int i = 1; i <= length; ++i) {
        for (int j = 1; j <= width; ++j) {
            if (i == 1 || i == length || j == 1 || j == width) {
                std::cout << "*";
            }
            else {
                std::cout << " ";
            }
        }
        std::cout << std::endl;
    }
}

// Circle implementation
Circle::Circle(double r) : radius(r) {}

double Circle::getArea() const {
    return M_PI * pow(radius, 2);
}

void Circle::draw() const {
    cout << "Drawing a circle with radius " << endl;
    cout << "   *****   " << endl;
    cout << " *       *   " << endl;
    cout << "*         *   " << endl;
    cout << " *       *    " << endl;
    cout << "   *****   " << endl;
}

// BMW implementation
void BMW::drive() const {
    cout << "Driving a BMW" << endl;
}

void BMW::draw() const {
    cout << "Drawing a BMW" << endl;
    std::cout << "    ______________" << std::endl;
    std::cout << " __/              \\_" << std::endl;
    std::cout << "/                   |" << std::endl;
    std::cout << "|                    \\_" << std::endl;
    std::cout << "\\__             BMW  _/" << std::endl;
    std::cout << "   \\___    ___    __/" << std::endl;
    std::cout << "       \\__/   \\__/" << std::endl;
}

// Mazda implementation
void Mazda::drive() const {
    cout << "Driving a Mazda" << endl;
}

void Mazda::draw() const {
    cout << "Drawing a Mazda" << endl;
    std::cout << "    ______________" << std::endl;
    std::cout << " __/              \\__" << std::endl;
    std::cout << "/                    |" << std::endl;
    std::cout << "|                    |" << std::endl;
    std::cout << "\\__            Mazda_/" << std::endl;
    std::cout << "   \\___    ___   __/" << std::endl;
    std::cout << "       \\__/   \\__/" << std::endl;
}

// Draw function for Shapes and Vehicles
void draw(const Shape& shape) {
    shape.draw();
}

void draw(const Vehicle& vehicle) {
    vehicle.draw();
}

// Drive function for Vehicles
void drive(const Vehicle& vehicle) {
    vehicle.drive();
}

// getTotalArea function
double getTotalArea(Shape** drawable, int size) {
    double totalArea = 0.0;
    for (int i = 0; i < size; i++) {
        totalArea += drawable[i]->getArea();
    }
    return totalArea;
}


这是h文件
#pragma once
class Drawable {
public:
    virtual double getArea() const = 0;
    virtual void draw() const = 0;
    virtual void drive() const = 0;
};
class Shape : Drawable
{
public:
    virtual double getArea() const = 0;
    virtual void draw() const = 0;
};

class Rectangle : public Shape {
private:
    double length, width;
public:
    Rectangle(double l, double w);
    double getArea() const override;
    void draw() const override;
};

class Circle : public Shape {
private:
    double radius;
public:
    Circle(double r);
    double getArea() const override;
    void draw() const override;
};

class Vehicle : Drawable {
public:
    virtual void drive() const = 0;
    virtual void draw() const = 0;
};

class BMW : public Vehicle {
public:
    void drive() const override;
    void draw() const override;
};

class Mazda : public Vehicle {
public:
    void drive() const override;
    void draw() const override;
};

void draw(const Shape& shape);
void draw(const Vehicle& vehicle);
double getTotalArea(Shape** shapes, int size);
void drive(const Vehicle& vehicle);

这是mian文件
#include 
#include 
#include "drawable.h"
using namespace std;



int main() {
    // Create two Rectangles and two Circles with different areas
    Rectangle rectangle1(5, 10);
    Rectangle rectangle2(3, 7);
    Circle circle1(2);
    Circle circle2(4);

    // Draw each shape
    draw(rectangle1);
    draw(rectangle2);
    draw(circle1);
    draw(circle2);

    // Get total area of all shapes
    Shape* shapes[] = { &rectangle1, &rectangle2, &circle1, &circle2 };
    int size = sizeof(shapes) / sizeof(shapes[0]);
    double totalArea = getTotalArea(shapes, size);
    cout << "The total area of all shapes is " << totalArea << endl;

    // Create a BMW and a Mazda
    BMW bmw;
    Mazda mazda;

    // Draw each vehicle
    draw(bmw);
    draw(mazda);
    
    // Drive each vehicle
    drive(bmw);
    drive(mazda);

    return 0;
}

总体来说就是我尝试把两个class(draw和drive)放在drawable class里面

这是一个 C++ 文件,其中定义了一些形状和车辆的类,并实现了 Drawable 基类中的一些虚函数和 draw() 函数。文件中还有一些其他函数,如 draw() 函数和 getTotalArea() 函数,它们将形状和车辆的实例作为参数,然后调用它们的虚函数来执行相应的操作。

Drawable 是一个抽象基类,定义了一个公共接口,其中包括三个纯虚函数:getArea()、draw() 和 drive()。Shape 和 Vehicle 类都继承自 Drawable 类,并在其上实现了具体的功能。

Shape 类有两个子类:Rectangle 和 Circle。Rectangle 类表示矩形,有一个长度和宽度,实现了 getArea() 和 draw() 函数来计算其面积和绘制图形。Circle 类表示圆形,有一个半径,实现了 getArea() 和 draw() 函数来计算其面积和绘制图形。

Vehicle 类也继承自 Drawable 类,但与 Shape 类不同的是,它有两个具体的子类:BMW 和 Mazda。这些类实现了 drive() 和 draw() 函数,以表示驾驶该车辆和绘制车辆的图形。

此外,文件还包括 draw() 和 drive() 函数,它们分别接受一个 Shape 或 Vehicle 实例并调用其 draw() 或 drive() 函数来执行相应的操作。getTotalArea() 函数接受一个 Shape 的数组和一个整数,计算数组中所有形状的面积总和并返回该值。

我应该怎么改呢?

该回答引用ChatGPT
根据提供的代码和错误信息,可以看出 C2259 错误是因为 Drawable 类中包含了纯虚函数 draw() 和 drive(),但是这些函数的实现只应该出现在派生类中,而不是在抽象基类中。

为了解决这个问题,您需要将 draw() 和 drive() 函数从 Drawable 类中移除,然后在 Shape 和 Vehicle 类中实现它们,如下所示:


class Shape : public Drawable {
public:
    virtual double getArea() const = 0;
    virtual void draw() const override = 0;
};

class Vehicle : public Drawable {
public:
    virtual void drive() const override = 0;
    virtual void draw() const override = 0;
};

注意到 Shape 和 Vehicle 类继承了 Drawable 类,因此它们必须实现 getArea() 和 draw() 函数。而 Vehicle 类还必须实现 drive() 函数。

此外,确保在 Rectangle,Circle,BMW 和 Mazda 类中实现 draw() 函数和 drive() 函数。

错误 C2259 通常表示类的定义不完整,无法实例化该类对象。在这种情况下,这可能是由于类定义不完整或未声明引用的头文件。在这种情况下,错误是在 cpp 文件中引发的。

在这里,问题是 Drawable 类没有虚析构函数,因此在调用析构函数时可能会出现问题。添加虚析构函数以解决问题:

virtual ~Drawable() {}

请记得在 h 文件中添加此声明,并在 cpp 文件中实现。
如果对您有帮助,请给与采纳,谢谢。