嗨,我有一些编程经验,但我对指针不是很好.我一直在尝试调试我一直在研究的这个程序,但它一直给我一个分段错误.我的代码如下:
#includeusing namespace std; class hexagon { public: hexagon(); ~hexagon(); void setSide(int side, hexagon *hexpiece); hexagon* getSide(int side); void setPos(int x, int y); int getX(); int getY(); void setID(int id); int getID(); private: hexagon *side0, *side1, *side2, *side3, *side4, *side5; int itsid, itsx, itsy; }; hexagon::hexagon() { side0 = NULL; side1 = NULL; side2 = NULL; side3 = NULL; side4 = NULL; side5 = NULL; } hexagon::~hexagon() { } void hexagon::setSide(int side, hexagon *hexpiece) { switch(side) { case 0: side0 = hexpiece; break; case 1: side1 = hexpiece; break; case 2: side2 = hexpiece; break; case 3: side3 = hexpiece; break; case 4: side4 = hexpiece; break; case 5: side5 = hexpiece; break; default: cout << "ERROR: Invalid side passed as argument" << endl; break; } } hexagon* hexagon::getSide(int side) { switch(side) { case 0: return side0; break; case 1: return side1; break; case 2: return side2; break; case 3: return side3; break; case 4: return side4; break; case 5: return side5; break; default: cout << "EROR: Invalide side passed as argument" << endl; cout << "Returning side0 by default" << endl; return side0; break; } } void hexagon::setPos(int x, int y) { itsx = x; itsy = y; } int hexagon::getX() { return itsx; } int hexagon::getY() { return itsy; } void hexagon::setID(int id) { itsid = id; } int hexagon::getID() { return itsid; } int main() { hexagon hexpieces[120]; int tempx, tempy; tempx = 0; tempy = 0; for(int i = 0; i<121; i++) { if(i%11 == 0) { tempx = 7*(i/11); tempy = 12*(i/11); } else { tempx = tempx + 14; } cout << "Setting hexpiece" << i << " x to " << tempx << " and y to " << tempy << endl; hexpieces[i].setPos(tempx, tempy); } for(int i=0; i<121; i++) { cout << "Setting hexpiece" << i << " id" << endl; hexpieces[i].setID(i); for(int j = 0;j<6; j++) { cout << "Setting hexpiece" << i << " side" << j << endl; if(j == 0 && i > 10 && i % 11 != 10) { hexpieces[i].setSide(j,&(hexpieces[i-10])); } else if(j == 1 && i % 11 != 10) { hexpieces[i].setSide(j,&(hexpieces[i+1])); } else if(j == 2 && i < 110) { hexpieces[i].setSide(j,&(hexpieces[i+11])); } else if(j == 3 && i % 11 != 0 && i < 110) { hexpieces[i].setSide(j,&(hexpieces[i+10])); } else if(j == 4 && i % 11 != 0) { hexpieces[i].setSide(j,&(hexpieces[i-1])); } else if(j == 5 && i > 10) { hexpieces[i].setSide(j,&(hexpieces[i-11])); } } } hexagon *itr1; itr1 = hexpieces; cout << "Hexpiece" << itr1->getID() << " side1 is connected to Hexpiece"; itr1 = itr1->getSide(1); cout << itr1->getID() << endl; cout << "Hexpiece" << itr1->getID() << " side2 is connected to Hexpiece"; itr1 = itr1->getSide(2); cout << itr1->getID() << endl; cout << "Hexpiece" << itr1->getID() << " side4 is connected to Hexpiece"; itr1 = itr1->getSide(4); cout << itr1->getID() << endl; return 0; }
我的问题似乎是代码的以下部分:
int tempx, tempy; tempx = 0; tempy = 0; for(int i = 0; i<121; i++) { if(i%11 == 0) { tempx = 7*(i/11); tempy = 12*(i/11); } else { tempx = tempx + 14; } cout << "Setting hexpiece" << i << " x to " << tempx << " and y to " << tempy << endl; hexpieces[i].setPos(tempx, tempy); }
当我编译代码并且它包含该部分时它运行程序但最后我得到了分段错误.但是,如果我注释掉那部分一切正常,并且没有分段错误.我不明白常规整数如何导致分段错误.如果有人能解释我犯了什么错误以及我做了什么,我会非常感激.提前致谢
hexpieces
是一个长度为120的数组,所以它的最大索引是119.你要hexpieces[i]
使用i
= 120(这是你的for
循环所采用的最后一个索引)访问.既然你没有"拥有"那段记忆,你就会得到一个分段.