我正在尝试使用子类“Circle”、“Triangle”、“Rectangle”创建一个父类“Shape”。父类保存 x 位置、y 位置和填充颜色或所有“形状”,然后每个子类保存特定于该形状的信息。有人会介意查看我的代码,看看为什么我在尝试设置对象数组中的半径时收到错误“形状没有成员'setRadius'”...
PS现在我只有子类“Circle”,直到我让它工作。然后我将添加其他两个类。
此外,如果有人在我的代码中看到任何其他错误,我将不胜感激。
提前致谢
#include <allegro.h>
#include <cstdlib>
using namespace std;
#define scrX 640
#define scrY 400
#define WHITE makecol(255,255,255)
#define GRAY makecol(60,60,60)
#define BLUE makecol(17,30,214)
int random(int low, int high);
const int numCircles = random(1,50);
class Shape{
public:
Shape(){x = scrX / 2; y = scrY / 2; fill = WHITE;}
protected:
int x, y, fill;
};
class Circle : public Shape{
public:
Circle(){radius = 0;}
Circle(int r){radius = r;}
void setRadius(int r){radius = r;}
protected:
int radius;
};
int main()
{
// Program Initialization
allegro_init();
install_keyboard();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, scrX, scrY, 0, 0);
// Create and clear the buffer for initial use
BITMAP *buffer = create_bitmap(scrX, scrY);
clear_to_color(buffer, GRAY);
// Set title and create label text in window
set_window_title("Bouncing Balls Ver 1.0");
textout_ex(buffer, font, "Bouncing Balls Ver 1.0", 10, 20, WHITE, GRAY);
// Draw a background box
rectfill(buffer, 50, 50, scrX-50, scrY-50, BLUE);
// Create circles
Shape **GCir;
GCir = new Shape *[numCircles];
for(int i=0;i<numCircles;i++){
GCir[i] = new Circle;
GCir[i]->setRadius(random(1,25)); // THIS IS THE ERROR
}
while(!key[KEY_ESC]){
blit(buffer, screen, 0, 0, 0, 0, scrX, scrY);
}
destroy_bitmap(buffer);
return 0;
}
END_OF_MAIN();
int random(int low, int high)
{
return rand() % (high - low) + low;
}