How to solve this error for this part :=>warning: control reaches end of non-void function [-Wreturn-type] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to solve this error for this part :=>warning: control reaches end of non-void function [-Wreturn-type]

bool Rover::move(Map &mars) { char Heading[] = {'^', '>', 'v', '<'}; switch (heading) { case 0: if (mapper.isHill(x, y+1)) { cout << "cant move" << endl; return false; } if(mapper.isTrap(x, y+1)){ mapper.setObject(x, y, ' '); y += 1; mapper.setObject(x, y, 164); system("cls"); mapper.display(); break; } if(!mapper.isInsideMap(x, y+1)){ cout << "hitting a wall"<< endl; return false; } else{ mapper.setObject(x, y, ' '); y += 1; mapper.setObject(x, y, Heading[heading]); collectgold(mars); return true; } case 1: if (mapper.isHill(x+1, y)) { cout << "cant move" << endl; return false; } if(mapper.isTrap(x+1, y)){ mapper.setObject(x, y, ' '); x += 1; mapper.setObject(x, y, 164); system("cls"); mapper.display(); break; } if(!mapper.isInsideMap(x+1, y)){ cout << "hitting a wall"<< endl; return false; } else{ mapper.setObject(x, y, ' '); x += 1; mapper.setObject(x, y, Heading[heading]); collectgold(mars); return true; } case 2: if (mapper.isHill(x, y-1)) { cout << "cant move" << endl; return false; } if(mapper.isTrap(x, y-1)){ mapper.setObject(x, y, ' '); y -= 1; mapper.setObject(x, y, 164); system("cls"); mapper.display(); break; } if(!mapper.isInsideMap(x, y-1)){ cout << "hitting a wall"<< endl; return false; }

3rd Mar 2021, 7:51 PM
Sky Yogash Shri
Sky Yogash Shri - avatar
1 Answer
+ 4
I can't say for sure because your program has been cut off due to the characer limit. Please save this code in C++ code in the code playground and then link it here. From what I see, this is the problem: The warning is there because the compiler detected that there is a possibility that your function ends without returning anything. That possibility is when nome of the `case` conditions are matched. Put a default case in the switch block and return something from the default case and the warning will go away.
3rd Mar 2021, 8:47 PM
XXX
XXX - avatar