涉及类的c ++代码中无法执行的错误

时间:2013-05-12 17:31:28

标签: c++ class abstract-class

我编写了一个包含以下类的c ++程序:class 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 ();
    }
    }

......还有一堆类似的条件。

我的编译器在b和d下给出了一个错误,即main中的Rectangle对象和Ellipse对象声明。错误是“预期的';'”这个“;”属于超越我,任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我可以用g ++编译,在main之后添加一个'}'。不过我没有graphics.h。