Error not all control paths return a value

Jun 16, 2020 at 10:36pm
Hye! I am trying to visualize 3 data structures. It's giving an error C4715 "Bst search, not all control paths return a value" .


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

int Search(int v, int index)
	{
		if (index >= size)
			return -1;
		else if (v != arr[index])
		{
			if (v < arr[index])
				Search(v, (2 * index) + 1);
			else if (v > arr[index])
				Search(v, (2 * index) + 2);
		
		}
		else if (v = arr[index])
			return index;
	}
Jun 16, 2020 at 10:55pm
This is what the compiler sees:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int Search(int v, int index)
{
  if (index >= size) {
    return -1;
  }
  else if (v != arr[index]) {
    // no return
  }
  else if (v = arr[index]) {
    return index;
  }
  else {
    // no return
  }
}
Jun 17, 2020 at 1:50pm
Hello lost110,

It has been my experience that the C4715 has been a warning and not an error.

As a warning this will not stop the program from compiling or running, but does warn of a potential problem.

I have also found that it usually refers to the end of the function and not, with your case the if/else if statements.

That being said this should help:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int Search(int v, int index)
{
    if (index >= size)
        return -1;
    else if (v != arr[index])
    {
        if (v < arr[index])
            Search(v, (2 * index) + 1);
        else if (v > arr[index])
            Search(v, (2 * index) + 2);
    }
    else if (v = arr[index])
        return index;

    // <--- Nothing returned here. What the warning is complaining about.
}


I did not have enough information to test the function like:
 • Where is "arr" defined and how?
 • What is "v" and what value does it have? "v" needs a better name.
 • What value does "index" have when the function is first called?

The other part i will mention is that "=" means set and "==" means compare. Look closer at your code.

Andy
Topic archived. No new replies allowed.