TREE HELP!!!

D

Don Hedgpeth

So I have this piece of code running and it comes from a program posted
last night under nested problems.

When I run this piece the program will return that the conditional on the
line after the for loop is true and print but never enters the scope of
the nested if statment.

Please any help will be greatly appreciated.


TreeNode * getFlagNode() {
printf("IN GETFLAG!\n");
vector< TreeNode *>::iterator p1;
for(p1 = children->begin();p1 !=
children->end();++p1){
printf("%d\n", (*p1)->payload->getX()==maze->get_flag_row() &&
(*p1)->payload->getY()maze->get_flag_col());
if((*p1)->payload->getX()
== maze->get_flag_row() && (*p1)->payload->getY() == maze->get_flag_col())
{

printf("RETURN NEXT");

return *p1;
}
}
}
 
K

Kai-Uwe Bux

Don said:
So I have this piece of code running and it comes from a program posted
last night under nested problems.

When I run this piece the program will return that the conditional on the
line after the for loop is true and print but never enters the scope of
the nested if statment.

Please any help will be greatly appreciated.


TreeNode * getFlagNode() {
printf("IN GETFLAG!\n");
vector< TreeNode *>::iterator p1;
for(p1 = children->begin();p1 !=
children->end();++p1){
printf("%d\n", (*p1)->payload->getX()==maze->get_flag_row() &&
(*p1)->payload->getY()maze->get_flag_col());

Shouldn't that be:

(*p1)->payload->getY() == maze->get_flag_col());

if((*p1)->payload->getX()
== maze->get_flag_row() && (*p1)->payload->getY() == maze->get_flag_col())
{

printf("RETURN NEXT");

return *p1;
}
}
}


Best

Kai-Uwe Bux
 
C

Chad E. Dollins

I think that the code goes as follows:
TreeNode * done;
{
printf("Found the flag\n");
done = ((*myMoves)[x])->getFlagNode();
printf("PEACE WE'RE OUT!");
}

....

TreeNode * getFlagNode() {
vector< TreeNode *>::iterator p1;
for(p1 = children->begin();p1 != children->end();++p1){
printf("%d\n",((*p1)->payload->getX() ==
maze->get_flag_row() && (*p1)->payload->getY() ==
maze->get_flag_col()));
if((*p1)->payload->getX() == maze->get_flag_row() &&
(*p1)->payload->getY() == maze->get_flag_col()){
printf("RETURN FLAG!\n");
return *p1;
}
}
}

output looks like this:
Found the flag
1
RETURN FLAG!


What don't understand is what happens to the program is does fault it just
hangs maybe someone can help me see what is wrong because I am new to c++
this type of runtime error does not mean anything to me.
 
J

John Harrison

Chad said:
I think that the code goes as follows:
TreeNode * done;
{
printf("Found the flag\n");
done = ((*myMoves)[x])->getFlagNode();
printf("PEACE WE'RE OUT!");
}

...

TreeNode * getFlagNode() {
vector< TreeNode *>::iterator p1;
for(p1 = children->begin();p1 != children->end();++p1){
printf("%d\n",((*p1)->payload->getX() ==
maze->get_flag_row() && (*p1)->payload->getY() ==
maze->get_flag_col()));
if((*p1)->payload->getX() == maze->get_flag_row() &&
(*p1)->payload->getY() == maze->get_flag_col()){
printf("RETURN FLAG!\n");
return *p1;
}
}
}

output looks like this:
Found the flag
1
RETURN FLAG!


What don't understand is what happens to the program is does fault it
just hangs maybe someone can help me see what is wrong because I am new
to c++ this type of runtime error does not mean anything to me.

Since your program is hanging when you execute a return statement the
most likely thing is that you've corrupted the program stack.

Hard to see what might have caused that in the code you've posted but
the usual reason is that you're using an array somewhere and you've
written past the end of that array.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top