我已经尝试使以下测试程序工作2天,但它不起作用.它基于几个头文件完全正常工作,因为我通过另一个测试程序检查它们.它有头文件,称为Area,Circle,Ring,Rectangle和Square.我还定义了函数randcolor和randsize; 我一遍又一遍地检查每一个,但它在while循环中第二次尝试后产生相同的ouptut:
int main()
{
srand(time(NULL));
Area *list[20];
int m;
Area *c;
int j = 0;
while (j < 20) {
m = rand() % 4;
cout << m << endl;
switch (m) {
case 0: {
Circle a(randcolor(), randsize());
c = &a;
break;
}
case 1: {
Ring r(randcolor(), randsize(), randsize());
c = &r;
break;
}
case 2: {
Rectangle re(randcolor(), randsize(), randsize());
c = &re;
break;
}
case 3: {
Square sq(randcolor(), randsize());
c = &sq;
break;
}
}
list[j] = c;
j++;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
请帮帮我预期的输出应该是:2区域构造函数被调用.. 1区域构造函数被称为0区域构造函数被调用
所以它应该是:0到3之间的20次随机数"区域构造函数被称为......"
但它在第二次尝试后给出了相同的数字......在while循环中
不知道你想要多少深度,问题是你将临时对象的无效地址推入列表中.在输入每个案例时,将创建,处理,然后销毁生成的对象.该地址可能会在下一个循环中重用,但只要剩下最后一个对象的范围就会无效.
试试这个:
int main()
{
srand ( time(NULL) );
Area *list[20];
int j=0;
while(j < sizeof(list)/sizeof(*list)))
{
switch ( rand() % 4 )
{
case 0:
{
list[j++] = new Circle(randcolor(),randsize());
break;
}
case 1:
{
list[j++] = new Ring(randcolor(),randsize(),randsize());
break;
}
case 2:
{
list[j++] = new Rectangle(randcolor(),randsize(),randsize());
break;
}
case 3:
{
list[j++] = new Square(randcolor(),randsize());
break;
}
}
}
// TODO: Use your Area array list
// cleanup
for (int i=0; i<j; ++i)
delete list[i];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我建议花些时间学习动态分配.一旦你这样做,花更多的时间来学习标准库容器和智能指针,可以减轻你的大部分头痛.
替代方案(迟早你会想要学习这些东西)
#include <iostream>
#include <vector>
#include <memory>
#include <random>
// include your Area and other declarations here
int main()
{
std::random_device rd;
std::mt19937 rng(rd());
std::uniform_int_distribution<> dist(0,3);
std::vector< std::unique_ptr<Area> > list;
for (int i=0; i<20; ++i)
{
switch ( dist(rng) )
{
case 0:
list.emplace_back(new Circle(randcolor(),randsize()));
break;
case 1:
list.emplace_back(new Ring(randcolor(),randsize(),randsize()));
break;
case 2:
list.emplace_back(ew Rectangle(randcolor(),randsize(),randsize()));
break;
case 3:
list.emplace_back(new Square(randcolor(),randsize()));
break;
}
}
// TODO: Use your Area array list
for (auto& ptr : list)
{
ptr->Method();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)