C++ 突然,对于特定对象的一个特定函数,在c++;
下面是代码片段C++ 突然,对于特定对象的一个特定函数,在c++;,c++,pointers,exc-bad-access,C++,Pointers,Exc Bad Access,下面是代码片段 Obj *temp; temp = new Sphere(center, radius); struct color c = {1, 0, 0}; temp->setColor(c); //works fine temp->setShine(1); //works fine temp->setCoefficients(0.4, 0.2, 0.2, 0.2); //works fine temp->draw(); //NULL pointer!! objec
Obj *temp;
temp = new Sphere(center, radius);
struct color c = {1, 0, 0};
temp->setColor(c); //works fine
temp->setShine(1); //works fine
temp->setCoefficients(0.4, 0.2, 0.2, 0.2); //works fine
temp->draw(); //NULL pointer!!
objects.push_back(temp);
temp = new Floor(1000, 20);
temp->draw(); //works fine too!
objects.push_back(temp);
其中Obj是基类,球体和地板是派生类,draw是虚拟方法
在此之前,我的绘图函数工作正常,没有指针问题。突然,我遇到了这个exc错误访问错误,我调试并发现无论从哪里调用Sphere::draw(),“this”指针都变为null
(在这里正确粘贴Sphere::draw()函数时遇到问题。)
glPushMatrix();
{
GL3F(c.r、c.g、c.b);
glTranslatef(referencePoint.x,referencePoint.y,referencePoint.z);
printf(“内部球体绘制\n”);
双半径=长度;
结构点点[1000][1000];
整数堆栈=1000,切片=1000;
int i,j;
双h,r;
//生成点
对于(i=0;i指针未为空,您只需尝试写入超出数组边界的内容。此处:
for(i=0;i<=stacks;i++)
在这里:
glVertex3f(points[i][j+1].x,points[i][j+1].y,-points[i][j+1].z);
glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,-points[i+1][j+1].z);
glVertex3f(points[i+1][j].x,points[i+1][j].y,-points[i+1][j].z);
那些[i+1]
,[j+1]
部分正试图在上一次迭代中执行与上面相同的操作。错误在于您以前对代码所做的更改。在调试器中为temp设置一个观察点。是的,事实上我刚才已经找到了。谢谢您:)不客气。如果你认为这个答案解决了你的问题,你可以接受。
for(j=0;j<=slices;j++)
glVertex3f(points[i][j+1].x,points[i][j+1].y,points[i][j+1].z);
glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,points[i+1][j+1].z);
glVertex3f(points[i+1][j].x,points[i+1][j].y,points[i+1][j].z);
glVertex3f(points[i][j+1].x,points[i][j+1].y,-points[i][j+1].z);
glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,-points[i+1][j+1].z);
glVertex3f(points[i+1][j].x,points[i+1][j].y,-points[i+1][j].z);