Hi I have some experience with programming but I'm not very good with pointers. I've been trying to debug this program I've been working on but it keeps giving me a segmentation fault. My code is the following:
#include <iostream>
using 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;
}
My problem seems to be with the following part of the code:
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);
}
When I compile the code and it includes that section it runs the program but then at the end I get a segmentation fault. However, if I comment out that section everything runs fine and there is no segmentation fault. I don't understand how a regular integer could be causing a segmentation fault. If someone could explain what mistake I made and where I made it I would greatly appreciate it. Thanks in advance