c++代码中涉及类的无法追踪的错误
Untraceable error in c++ code involving classes
我写了一个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
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- C++:光线追踪器导致内存不足错误 - 由于反复调用"new"?
- 错误我无法追踪
- c++代码中涉及类的无法追踪的错误
- 我无法追踪可能的复制构造错误