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;
}

C2259 是指派生类在继承基类时缺少必要的定义,需要在派生类中添加缺失的定义。在这个代码中,错误出现在 Drawable 类和 Shape 类的继承中,因为 Drawable 类没有一个具体的实现,所以需要在 Shape 类中为纯虚函数添加实现。

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

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

virtual ~Drawable() {}

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

该回答引用ChatGPT

在h文件中,Drawable类定义了三个纯虚函数,即getArea()、draw()和drive(),但这些函数在后面的派生类中都没有实现。因此,需要在每个派生类中实现这些函数。

具体地,Rectangle类和Circle类都继承自Shape类,在Shape类中已经定义了getArea()和draw()函数,因此不需要重新定义。但需要在Rectangle类和Circle类中实现这些函数。对于Vehicle类的两个派生类BMW和Mazda,也需要在这两个派生类中实现drive()和draw()函数。

修改后的代码如下:

#pragma once

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

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

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 : public 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);


#include "drawable.h"
#include <iostream>
#include <cmath>

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