Omar Stevens
Omar Stevens

Reputation: 1

Keep getting error message not all control paths return a value

bool isEnemy(const string& check) {
  if (check == enemy1 || check == enemy2 ||
      check == enemy3)  // if the name being checked is an enemy of this knight
    return true;
  else
    return false;
}

int Canidate(Knight b) {  //'b' is an enemy of 'a' who is in the seat before
                          //'b'(ie. b seat=2 a seat=1
  int toSwap = b.seatingPos;  // toSwap holds seating pos # of 'b'
  int checkFriends = (toSwap - 1);  // holds seating pos of 'a'
  for (int i = 0; i < 8; i++) {
    if (!(table[i].isEnemy(table[checkFriends].getName())))  // if not enemies,
                                                             // then must be
                                                             // friends
    {
      friends.push_back(table[i].seatingPos);  // adds seating # of friends of
                                               // 'a' to friends vector
    }
  }
  for (int j = 0; j < 3; j++) {  // check friends of 'a' to see if their
                                 // neighbor is friends with 'b'
    int check2 =
        table[friends[j]].seatingPos;  // check 2 holds seating pos # of 'c'
    if (!(table[toSwap].isEnemy(
            table[(friends[j] + 1)]
                .getName()))) {  // if neighbor of c is friends with b(toSwap)
      return check2;  // if not enemies then must be friends return seating pos
                      // of acceptable canidate
    }
  }
}

table is a vector<Knight>. friends is a vector<int>

Upvotes: 0

Views: 52

Answers (1)

bhristov
bhristov

Reputation: 3187

This is a problem which occurs when there is a way for your function to skip over the cases in which you have specified a return.

This is an example of this:

int funct() {
    int a = 3;
    if(a == 4) {
        return a;
    }
}

if "a" is 4 then we know what to return. But if "a" is not 4 the function does not know what to return. To correct this you need to give the function a return statement that will cover the rest of the cases that are possible.

int funct() {
    int a = 3;
    if(a == 4) {
        return a;
    }
    return -1;
}

this way all paths that the function can take have been covered and the function knows what to return if it "a" is not equal to 4.

Upvotes: 2

Related Questions