【发布时间】:2015-05-07 07:18:35
【问题描述】:
所以,这是我的代码:
#include <iostream>
#include <conio.h>
using namespace std;
class rectangle {
public:
double width;
double height;
rectangle(double, double);
double area() { return (width*height);}
};
rectangle::rectangle(double a, double b) {
width = a;
height = b;
}
int main() {
cout << "How many rectangles would you like to create? ";
int rectNum;
cin >> rectNum;
for (int counter = 0; counter < rectNum; counter++) {
int rectCount = 1;
rectCount = counter + 1;
double rectWidth, rectHeight;
cout << "\nEnter width of rectangle " << rectCount << ": ";
cin >> rectWidth;
cout << "\nEnter height of rectangle " << rectCount << ": ";
cin >> rectHeight;
rectangle rect/*integer value of rectCount at current time*/(rectWidth, rectHeight);
}
return 0;
}
正如评论部分所说,我想创建一个名为 rect 的矩形,其后缀是整数 rectCount 的当前值。我该怎么做?提前致谢!
【问题讨论】:
-
数组、向量等怎么样?
-
可能有更好的方法来完成您想要实现的任何目标。你原来的问题是什么?你需要这个做什么?
-
@deviantfan 当我尝试使用数组时,它给出了错误“表达式必须具有常量值”。尝试使用 std::vector
根本不起作用。没有错误信息或任何东西。只是一条红色的波浪线。 -
@tux3 我没有这样做的理由,只是学习 C++(特别是类)。
-
结构体内部应该有输入输出函数。