disaste0_0 2023-02-20 12:13 采纳率: 81.6%
浏览 49
已结题

c++ 编译器错误 C2259应该怎么改呢

可以帮忙看看应该怎么改嘛


这是cpp文件
#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;
    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 <iostream>
#include <cmath>
#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;
}

  • 写回答

3条回答 默认 最新

  • 量化研究所 2023-02-20 12:16
    关注

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

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月20日
  • 创建了问题 2月20日

悬赏问题

  • ¥30 电脑误删了手机的照片怎么恢复?
  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。
  • ¥20 学生成绩管理系统设计
  • ¥15 来一个cc穿盾脚本开发者
  • ¥15 CST2023安装报错
  • ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办
  • ¥15 有人懂怎么做大模型的客服系统吗?卡住了卡住了
  • ¥20 firefly-rk3399上启动卡住了