C++ 如何在C++;?
我正试图完成一个家庭作业,创建一个形状类层次结构。在main()中,我试图填充指向形状对象的指针数组。然后,在填充之后,我需要遍历该数组,并对每个对象指针调用print()(这里称为“SendToString”)函数 当运行这个程序时,它会在运行时的第二个for循环中崩溃,就在我输入一些形状,然后输入“done”之后。它似乎不能在shape_数组中循环 在getShape()中,如果用户在控制台中键入“done”,则返回空指针。当从getShape()返回空指针时,main()中的循环应该退出并停止填充数组 我的代码有什么问题?我仔细研究了几个小时,看不出问题所在。谢谢C++ 如何在C++;?,c++,pointers,C++,Pointers,我正试图完成一个家庭作业,创建一个形状类层次结构。在main()中,我试图填充指向形状对象的指针数组。然后,在填充之后,我需要遍历该数组,并对每个对象指针调用print()(这里称为“SendToString”)函数 当运行这个程序时,它会在运行时的第二个for循环中崩溃,就在我输入一些形状,然后输入“done”之后。它似乎不能在shape_数组中循环 在getShape()中,如果用户在控制台中键入“done”,则返回空指针。当从getShape()返回空指针时,main()中的循环应该退出并
#include "stdafx.h"
#include <iostream>
#include "Shape.h"
#include <string>
using namespace std;
Shape* getShape();
int main()
{
int i; //fill the shape array.
int j; //loop through the shape array.
string output;
Shape* shape_array[15]; //array of base Shape pointers
Shape *shape_ptr;
int nElements = sizeof(shape_array) / sizeof(shape_array[0]);
cout << "Enter a list of shapes - 'done' to end" << endl;
//fill the array of shape object pointers
for (i =0; i <= nElements; i++) { //loop through the defined array of 15 elements
shape_ptr = getShape(); //return a shape ptr or NULL ptr
if (shape_ptr== NULL)
{
break; //stop filling the array (stop calling getShape)
}
else
{
shape_array[i]= shape_ptr; //add the shape pointer
}
} //end for
cout << "The list of shapes entered..." << endl;
for (j = 0; j < nElements; j++) {
output = shape_array[j]->SendToString();
cout << output << endl;
}
return 0;
} //end main
它无法打印第二个对象,并在那里崩溃。就像胡安科潘扎在评论中说的那样,你正在访问数组
for (j = 0; j <= nElements; j++) {
然后我可以使用I[0]访问第一个元素,使用I[1]访问第二个元素。这是所有2个元素访问,我还没有访问I[2]。C/C++数组中可接受的索引范围始终为0到(nElements-1)
见此:
还请注意,在最后一个循环中,当尚未为阵列中的所有15个点输入形状时,将遍历整个阵列。这会导致您试图访问垃圾指针指向的内存,因此很可能未映射。这极有可能导致坠机 除了第一个循环中明显的越界访问之外,还可以处理整个数组,就好像它的所有元素都指向有效对象一样:
for (j = 0; j < nElements; j++) {
output = shape_array[j]->SendToString();
在第一个循环中,只设置数组中的第二个元素,但取消引用所有元素。您正在反引用无效指针。如果要编写此类代码,应该学习使用调试器。数组索引的所有有效范围的第一个是
[0,nElements-1]
其次,两个循环都写得不正确,并且包含错误。例如,在第一个循环中,变量i递增两次
正确的代码可以如下所示
#include "stdafx.h"
#include <iostream>
#include <string>
#include "Shape.h"
Shape *getShape(); // Why this function is declared here but not is defined in the module?!
int main()
{
const int N = 15;
Shape* shape_array[N]; //array of base Shape pointers
std::cout << "Enter a list of shapes - 'done' to end" << std::endl;
//fill the array of shape object pointers
Shape *shape_ptr;
int nElements = 0;
for ( ; nElements < N && ( shape_ptr = getShape() ); nElements++ )
{
//loop through the defined array of 15 elements
shape_array[nElements] = shape_ptr; //add the shape pointer
} //end for
std::cout << "The list of shapes entered..." << std::endl;
for ( int i = 0; i < nElements; i++ )
{
std::string output = shape_array[i]->SendToString();
std::cout << output << std::endl;
}
return 0;
}
#包括“stdafx.h”
#包括
#包括
#包括“Shape.h”
Shape*getShape();//为什么这个函数在这里声明,但没有在模块中定义?!
int main()
{
常数int N=15;
Shape*Shape_数组[N];//基本形状指针数组
std::您是否正在访问超出边界的数组。查看此循环:for(i=0;i IfgetShape
返回动态分配的Shape
s,您可能正在泄漏它们。如果没有,您可能有悬空指针。将循环绑定更改为(i=0;iint i[2];
for (j = 0; j < nElements; j++) {
output = shape_array[j]->SendToString();
for (i =0; i < nElements; i++)
{
shape_array[i]= something_good;
i++; // LOOK! Index increased one extra time per loop!!!
}
#include "stdafx.h"
#include <iostream>
#include <string>
#include "Shape.h"
Shape *getShape(); // Why this function is declared here but not is defined in the module?!
int main()
{
const int N = 15;
Shape* shape_array[N]; //array of base Shape pointers
std::cout << "Enter a list of shapes - 'done' to end" << std::endl;
//fill the array of shape object pointers
Shape *shape_ptr;
int nElements = 0;
for ( ; nElements < N && ( shape_ptr = getShape() ); nElements++ )
{
//loop through the defined array of 15 elements
shape_array[nElements] = shape_ptr; //add the shape pointer
} //end for
std::cout << "The list of shapes entered..." << std::endl;
for ( int i = 0; i < nElements; i++ )
{
std::string output = shape_array[i]->SendToString();
std::cout << output << std::endl;
}
return 0;
}