我刚刚开始学习C ++,我无法弄清楚为什么rand()在这里不能用于rect0对象。当我运行程序时,它不会像我想象的那样随机绘制rect0。它每次都打印在同一个位置。这是代码:
class Rect
{
Vec2 min, max;
public:
Rect(int minx, int miny, int maxx, int maxy)
:min(minx, miny), max(maxx, maxy)
{}
Rect() {}
void draw(const char letter) const
{
for (int row = min.y; row < max.y; row++)
{
for (int col = min.x; col < max.x; col++)
{
if (row >= 0 && col >= 0)
{
moveCursor(col, row);
putchar(letter);
}
}
}
}
void setRandom(Rect & r)
{
r.min.x = rand() % 5;
r.max.x = rand() % 10 + 10;
r.min.y = rand() % 2;
r.max.y = rand() % 10 + 10;
}
int main()
{
Rect rect0;
rect0.setRandom(rect0);
rect0.draw('0');
moveCursor(0, 0); // re-print instructions
return 0;
}