我这里有这段代码
string shapename;
bool warpspace;
const int size = 100;
ShapeTwoD shape2D[size]; // ShapeTwoD is a parent class
int number =0;
static int count = 0;
while(choice !=999)
{
switch(choice)
{
case 1:
{
cout<<"[Input sensor data]"<<endl;
cout<<"Please enter name of Shape:";
cin>>shapename;
shape2D[size].setName(shapename); //segmentation fault
cout<<"Please enter special type:";
cin>>warpspace;
shape2D[size].setContainsWarpSpace(warpspace);
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if(shapename == "Square" || shapename == "square")
{ square.setSquareCord(); count++; }
if(shapename == "Rectangle" || shapename == "rectangle")
{ rect.setRectangleCord(); count++; }
mainmenu();
cin>>number;
option(number);
}break;//there are still multiple cases
}
}
当我尝试编译并运行它时,它在我输入Shape please enter name of shape :
之后立即给出了分段错误我在这里做错了什么?
答案 0 :(得分:2)
您正在访问数组边界:
shape2D[size].
^^^^
有效数组索引从0到size-1