Shape *shape[100];//global scope
Square sqr;//global scope
void inputdata() {
int len,width;
cout << "enter length";
cin >> len;
cout << "enter width";
cin >> width;
Square sqr(len,width);
shape[0] = &sqr;
//----> if shape[0]->computeArea(); here works fine.
}
void computeArea() {
shape[0]->computeArea(); // --> run fail error
}
Shape 是父类,square 是子类。两者都有 computeArea();
当代码到达 computeArea() 时,我遇到了一个奇怪的运行失败错误。该程序只是终止而没有给我任何错误让我找到并修复它......它只是显示运行失败并停止程序。
如果代码在 inputdata() 内,则程序能够正常运行并显示 ->computeArea() 但是当我将它分开时,它只是无法正常运行。有什么解决办法吗?