Hi, I am really lost here, I have airport class which should navigate planes, in its list to runways, with method move, theres a method prepare which changes the direction of flight to all planes, always before move is called, move just increments decrement x and y of plane in its list. But after calling two times in row airport->move(), I get screwed and I really dont know wheres the problem. Have I badly initiazed something? Iterator gets invalidated.
Thanks for help.
Valgrind Stacktrace
Conditional jump or move depends on uninitialised value(s)
==26207== at 0x409601: plane::move() (in /home/xnovak11/Downloads/airport/main)
==26207== by 0x401FBD: airport::move() (in /home/xnovak11/Downloads/airport/main)
==26207== by 0x405FE1: io::start(std::istream&, std:
stream&, std:
stream&) (in /home/xnovak11/Downloads/airport/main)
This is how I add planes into list in airport
Code:
list<plane*> planes;
list<plane*>::const_iterator planeIterator;
list<plane*>::iterator planeIteratorMoj;
bool airport::addPlane(const plane& p)
{
for(planeIteratorMoj= planes.begin(); planeIteratorMoj!= planes.end(); planeIteratorMoj++)
{
if(p.getName()== (*planeIteratorMoj)->getName())
{
return false;
}
}
plane *p1 = new plane(p);
if(planes.empty())
{
planes.push_back(p1);
if(planes.size()==1)
{
planeIterator = planes.begin();
}
}
else
{
planes.push_back(p1);
planeIterator = planes.begin();
}
return true;
}
This is the method where it fails.
When I call it once, no problem, after second call I get instead of normal number in cout<<after move<< s1 i get like 8795456
Dunno why.
Code:
void airport::move()
{
for(planeIteratorMoj = planes.begin(); planeIteratorMoj!= planes.end(); planeIteratorMoj++)
{
plane * p1 = (*planeIteratorMoj);
int s,w;
p1->getPosition(s,w);
cout<<"before move function"<<s<<","<<w<< p1->getDirection()<<endl;
if((*planeIteratorMoj)->move())
{
delete *planeIteratorMoj;
}
plane * p2 = (*planeIteratorMoj);
int s1,w1;
p2->getPosition(s1,w1);
cout<<"after move function"<<s1<<","<<w1<< p2->getDirection()<<endl;
}
}
This is plane class, the copy constructor and the move method.
Code:
enum DIRECTION
{
NORTH = 0,
EAST = 1,
SOUTH = 2,
WEST = 3
};
string _name;
int _x;
int _y;
DIRECTION _direction;
bool _hasLanded;
DIRECTION previous;
plane::plane(const std::string& name, int x, int y, DIRECTION direction)
{
_name=name;
_x=x;
_y=y;
_direction=direction;
previous = direction;
}
/**
Copy constructor
@param other Airplane to make copy of
*/
plane::plane(const plane& other)
{
_name=other.getName();
other.getPosition(_x,_y);
_direction=other.getDirection();
_hasLanded = other._hasLanded;
previous = _direction;
}
bool plane::move()
{
cout<<"plane move"<<_x<<","<<_y<<" "<<getDirection()<<endl;
switch(_direction)
{
case 0 :
_y++;
break;
case 1 :
_x++;
break;
case 2 :
_y= _y -1;
break;
case 3 :
_x=_x - 1;
break;
}
if(_hasLanded)
{
return false;
}
return true;
}