Image

Imageallroy wrote in Imagecpp

Wierd error


 


Anyone have any idea why this doesn't work:


 


bool find(GTNode<string>* subroot, string p, bool f) {


if (subroot == NULL) return f;


if (subroot->GetValue().compare(p) == 0){


f = true;


}


find(subroot->Get_leftmost_child(),p,f);


find(subroot->Get_right_sibling(),p,f);


}


 


When I step through the function, it appears to hold the right value. When it finds a match it's also being set to true, so it actually seems like it's working correctly. The problem is it always returns the same thing, no matter what. Even if bool is set to 0 in it's last loop(I've checked) it returns a positive value to the function that calls it...  My question is why? I got around it by just making the flag a reference and it works fine, I'm just wondering what's wrong with this code?


 


This works properly:


void find(GTNode<string>* subroot, string p, bool &f) {


if (subroot == NULL) return;


if (subroot->GetValue().compare(p) == 0){


f = true;


}


find(subroot->Get_leftmost_child(),p,f);


find(subroot->Get_right_sibling(),p,f);


}


 


Thanks.