c++代码中涉及类的无法追踪的错误

Untraceable error in c++ code involving classes

本文关键字:追踪 错误 代码 c++      更新时间:2023-10-16

我写了一个c++程序,其中包含以下类:类Shape, Circle, Ellipse, Rectangle, Triangle。所有的类都是Shape类的子类。它们的语法定义是正确的,我没有看到明显的错误。这是代码:

#include "graphics.h"
//#include <iostream>
using namespace std;
const float Pi = 3.141;
float distance (int x1, int y1, int x2, int y2) {
    int x = x2 - x1;
    int y = y2 - y1;
    x = x * x;
    y = y * y;
    float total = x + y;
    float length = sqrt(total);
    return length;
}

class Shape {
public: 
    float area;  
    virtual void Area () = 0;
};
class Rectangle : public Shape {
public:
    int length, width;
    virtual void Area () {
        area = length * width;
    }
};
class Triangle : public Shape {
public:
    float a, b, c;
    virtual void Area () {
        float s = ((a + b + c) / 2);
        area = sqrt(s * (s - a) * (s - b) * (s - c));
    }
};
class Ellipse : public Shape {
public:
    int x, y; 
    float a, b; 
    virtual void Area () {
        area = Pi * a * b;
    }
};
class Circle : public Shape { 
public:
    int x, y; //Centre
    float radius; 
    virtual void Area () {
        area = Pi * radius * radius;
    }
};
class Line { //implement zigzag within Line
public:
    int x, y; //last point of line segment
};

//Function paintInterface to initalize the paint viewport and textboxes within the viewport
void paintInterface () {
    static int count = 0;
    //void initwindow (int width, int height, char * title)
    initwindow(1000, 800, "Paint");
    //void setbkcolor(int color)
    setbkcolor(WHITE);
    //void setcolor(int color)
    setcolor (BLACK);
    //void setfillstyle(int pattern, int color)
    setfillstyle (1, WHITE);
    //void bar(int left, int top, int right, int bottom)
    bar (0, 0, 1000, 800);
    //void outtextxy (int x, int y, char *textstring)
    outtextxy (2, 5, "New");
    //void rectangle(int left, int top, int right, int bottom)
    rectangle (0, 5, 35, 20);
    outtextxy (2, 25, "Circle");
    rectangle (0, 25, 40, 40);
    outtextxy (2, 45, "Rectangle");
    rectangle (0, 45, 67, 60);
    outtextxy (2, 65, "Triangle");
    rectangle (0, 65, 55, 80);
    outtextxy (2, 85, "Ellipse");
    rectangle (0, 85, 50, 100);
    outtextxy (2, 105, "Line");
    rectangle (0, 105, 30, 120);
    outtextxy (2, 130, "COLORS");
    setfillstyle (1, BLUE);
    bar (2, 150, 20, 160); 
    setfillstyle (1, RED);
    bar (25, 150, 43, 160);
    setfillstyle (1, GREEN);
    bar (2, 170, 20, 180);
    setfillstyle (1, YELLOW);
    bar (25, 170, 43, 180);
    setfillstyle (1, WHITE);
    outtextxy (2, 190, "Undo");
    rectangle (0, 190, 37, 205);
    outtextxy (2, 210, "Exit");
    rectangle (0, 210, 37, 225);

    if (count == 0) {
        outtextxy (400, 300, "Welcome to Paint!");
        delay(2000);
        count++;
        paintInterface ();
    }
}

int main ()
{
    Circle a;
    Rectangle b;
    Ellipse d;
    Triangle c;
    int x1, y1;
    float length;
    paintInterface ();
    int xcoord, ycoord;
    bool flag = true;
    while (flag) {
    while(!ismouseclick(WM_LBUTTONDOWN)) {}
    getmouseclick(WM_LBUTTONDOWN, xcoord, ycoord);
    if (((xcoord >= 0) && (xcoord <= 35)) && ((ycoord >= 5) && (ycoord <= 20))) {
        paintInterface ();
    }
    }

…还有很多类似的if条件

我的编译器在b和d下给出了一个错误,即main中的Rectangle对象和Ellipse对象声明。错误是,"Expected a '; " " Where this ";"属于超出了我,任何帮助将不胜感激。

我可以用g++在main之后加一个'}'来编译。我没有graphics。h