warning: control may reach end of non-void function

Your function returns a bool but there’s a potential code path (e.g. z.count is 0) where it may reach the end of the function and return nothing. Hence, the warning issued by the compiler.

Add return false; (or return true; but false seems appropriate with your current logic) at the end of your function.

As pointed out by others, your else part is also wrong. I’d rewrite it as:

bool operator<=(string a, const Zoo& z) {
// Pre: none
// Post: returns true if animal a is in Zoo z.
//       the owner does not count as an animal.

  for ( int i=0; i< z.count; i++ ) {
       if (z.cage[i] == a){
          return true;
        }
   }
   return false;
}

Leave a Comment