不知道为什么我对这段特定的代码行出现分段错误,我知道当您尝试访问您无法访问的内存部分但是我无法访问时出现此错误弄清楚什么是错的。分割故障核心转储C++
我想改变场景,并通过把矢量中的对象的功能,我得到分割错误(核心倾倒) ,这只发生在我推球体和平面,当我评论这2行出它的作品,但它不会渲染球体当然.. 任何想法? 它也适用,如果我删除“if语句” 谢谢。
vector < Source * > lightSource;
vector < Object * > sceneObjects;
while (!glfwWindowShouldClose(window)) {
if (firstScene) {
Sphere sphereScene(sphere, .825, green_ref);
Plane planeScene(plane[0][0], plane[0][1], -1, maroon);
Light lightScene(light, whiteLight);
////////////this is what is causing problem i think//////////////////
sceneObjects.push_back(dynamic_cast < Object * > (& sphereScene));
sceneObjects.push_back(dynamic_cast < Object * > (& planeScene));
/////////////////////////////////////////////////////////////////////
lightSource.push_back(dynamic_cast < Source * > (& lightScene));
for (int i = 0; i < 4; i++) {
sceneObjects.push_back(new Triangle(pyramidCoord[i], Blue));
}
for (int i = 0; i < 2; i++) {
sceneObjects.push_back(new Triangle(ceiling[i], White));
}
for (int i = 0; i < 2; i++) {
sceneObjects.push_back(new Triangle(wallG[i], Green));
}
for (int i = 0; i < 2; i++) {
sceneObjects.push_back(new Triangle(wallR[i], Red));
}
for (int i = 0; i < 2; i++) {
sceneObjects.push_back(new Triangle(floor1[i], White));
}
}
这是难以阅读的代码片段 - 似乎缺少括号(S)。如果缩进是正确的,那么Sphere,Plane和Light对象的生命周期将在if块之后结束。但悬停的指针现在未定义的内存存储在sceneObjects列表中,并可能在以后访问(和崩溃)。为什么不使用像Triangle这样的新产品? – Anders
好点我把它改成了这个sceneObjects.push_back(新球体(sphere,.825,green_ref)); sceneObjects.push_back(new Sphere(plane [0] [0],plane [0] [1],-1,m aroon)); lightSource.push_back(new Light(light,whiteLight));不是我得到不同的错误,但它不再分段错误 – blank
耶!下一次重要的是发布最小完整可验证示例(MVCE)http://stackoverflow.com/help/mcve,所以我们不必猜测。现在,到你的下一个问题;-) – Anders