Saturday, August 7, 2010

How would i fix this: warning: control reaches end of non-void function?

this keeps coming up when I compile the .cc part of my c program. The program is to implement a ternary tree and the problem is in regards to printing it out. The following is the part of code I'm having trouble with:





ostream %26amp; operator%26lt;%26lt;(ostream %26amp;out, const Tree %26amp; t){


return t.display(out, t.root);


}





ostream %26amp; Tree::display(ostream %26amp;out, Node *r)const{


if(r!=NULL){


display(out, r-%26gt;child[Node::L]);


out%26lt;%26lt;*r%26lt;%26lt;endl;


display(out, r-%26gt;child[Node::M]);


out%26lt;%26lt;*r%26lt;%26lt;endl;


display(out, r-%26gt;child[Node::R]);


}


}





I've tried putting a return at the end of the display function, but I'm not sure what to return. Thanks in advance for any help you can provide.How would i fix this: warning: control reaches end of non-void function?
return out;





I think that's what you're looking for. You need to put it at the end of the display function.





EDIT:





Ok, did you try this:


1) change the return type of the display function to void


2) change the first function so that it contains a call to the display function THEN a separate 'return out' statement. It should look something like:





ostream %26amp; operator%26lt;%26lt;(ostream %26amp;out, const Tree %26amp; t){


t.display(out, t.root);


return out;


}





void Tree::display(ostream %26amp;out, Node *r)const{


if(r!=NULL){


display(out, r-%26gt;child[Node::L]);


out%26lt;%26lt;*r%26lt;%26lt;endl;


display(out, r-%26gt;child[Node::M]);


out%26lt;%26lt;*r%26lt;%26lt;endl;


display(out, r-%26gt;child[Node::R]);


}


}





PLEASE note that if there are any declarations or prototypes of these functions, you will have to change any return values in them as well.How would i fix this: warning: control reaches end of non-void function?
If you don't need to return anything then change the return type from ostream to void.





Edit: You should probably change both of them to void, if they aren't returning anything. The second obviously returns nothing, and if the first returns the second, it isn't returning anything either. So set them both to void.
Your display function is supposed to return on ostream object; however, you never actually return anything. This is why you are getting the error.

No comments:

Post a Comment