disaste0_0 2023-02-20 11:51 采纳率: 81.6%
浏览 51
已结题

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

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

  • 写回答

4条回答 默认 最新

  • MarkHan_ 2023-02-20 11:54
    关注

    这是一个 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 的数组和一个整数,计算数组中所有形状的面积总和并返回该值。

    评论 编辑记录

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 关于#单片机#的问题:以ATMEGA128或相近型号单片机为控制器设计直流电机调速的闭环控制系统(相关搜索:设计报告|软件设计|流程图)
  • ¥15 打开软件提示错误:failed to get wglChoosePixelFormatARB
  • ¥30 电脑误删了手机的照片怎么恢复?
  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。
  • ¥20 学生成绩管理系统设计
  • ¥15 来一个cc穿盾脚本开发者
  • ¥15 CST2023安装报错
  • ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办