Cannot resolve overloaded function based on conversion to type

Hello everyone,

I am testing a function--viz., "mymaximum," which is supposed to give me the maximum value contained in a specified array. However, I am getting the following debug error:

"34:12: error: cannot resolve overloaded function ‘max’ based on conversion to type ‘int’ return max;"

Feedback would be much appreciated! Thanks in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <cassert>
using namespace std;

int mymaximum(int a[], int numberOfElements); //prototype

int main()
{
    int a[] = {12,0,-9,13,7,76};
    assert(mymaximum(a,6)== 76);

    int b[] = {13,0,7,98};
    assert(mymaximum(b,4)== 98);

    int c[] = {0,0,0,13};
    assert(mymaximum(c,4)== 13);

    int d[] = {-1,0,3,12};
    assert(mymaximum(d,4)== 12);

    cout << "All tests have successfully passed." << endl;
}

int mymaximum(int a[], int numberOfElements)
{                    
    for(int i=1;i<numberOfElements;++i)
    {
        int max = a[0];
        if(a[i] > max)
        {
            max = a[i];   
        }
    }
    return max;
}
Last edited on
Looks like this message is caused by
 
using namespace std;

The compiler cannot determine how you are trying to use std::max
http://www.cplusplus.com/reference/algorithm/max/

If you remove that using statement at line 3 and explicitly state std::cout and std::endl at line 21, the compiler will give a more helpful and appropriate error message.
Mine gives:
34	12	[Error] 'max' was not declared in this scope

Note that your variable int max exists only within the scope of the braces { } from lines 27 to 33.

When line 34 is reached, the variable named max no longer exists.

There is also a logic error (which the compiler cannot know about), the program should not reset max on every iteration of the loop, set its initial value just once before the start of the loop.


Last edited on
Thank you very much Chervil--that was very helpful!

It's good to know to know that "max" is already in the standard library.

I have moved that int max declaration such that when I "return max," the integer 'max' exists.

However, are you sure that there is a logic error? I made more assert-test cases (as shown below), and they all pass the test. So this amount of successful cases increases the probability that the relevant function's logic is correct. Can you come up with a counterexample case?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <cassert>
using namespace std;

int mymaximum(int a[], int numberOfElements); //prototype

int main()
{
    int a[] = {12,0,-9,13,7,76};
    assert(mymaximum(a,6)== 76);

    int b[] = {13,0,7,98};
    assert(mymaximum(b,4)== 98);

    int c[] = {0,0,0,13};
    assert(mymaximum(c,4)== 13);

    int d[] = {-1,0,3,12};
    assert(mymaximum(d,4)== 12);
    
    int e[] = {-9092,14,1,13,0};
    assert(mymaximum(e,5)== 14);

    int f[] = {-1,9800,3,12,-9006};
    assert(mymaximum(f,5)== 9800);
    
    int g[] = {88,1,1,88};
    assert(mymaximum(g,4)== 88);

    cout << "All tests have successfully passed." << endl;
}

int mymaximum(int a[], int numberOfElements)
{                    
    int max = a[0];
    for(int i=1;i<numberOfElements;++i)
    {
        if(a[i] > max)
        {
            max = a[i];   
        }
    }
    return max;
}
Last edited on
However, are you sure that there is a logic error?

No. I'm sure there was a logic error in the previous code.

Consider this:
1
2
3
4
5
6
7
8
9
10
11
12
int mymaximum(int a[], int numberOfElements)
{                    
    for(int i=1;i<numberOfElements;++i)
    {
        int max = a[0];
        if(a[i] > max)
        {
            max = a[i];   
        }
    }
    return max; // error, max does not exist
}

Now let's move the declaration outside the body of the loop. This will fix the compiler error:
1
2
3
4
5
6
7
8
9
10
11
12
13
int mymaximum(int a[], int numberOfElements)
{        
    int max;  // move the variable declaration          
    for(int i=1;i<numberOfElements;++i)
    {
        max = a[0];
        if(a[i] > max)
        {
            max = a[i];   
        }
    }
    return max; // ok, max is now visible
}

Above the original logic is preserved. Notice that your latest code does not follow this (incorrect) logic. Your version simultaneously changed both the scope of the declaration as well as the sequence in which the code is executed.


Sorry this is such a lengthy reply, I hope it makes sense. Anyway, I should reiterate, you did indeed correct the error.
Yes, that makes sense. I simultaneously changed the scope of the declaration and the logic of the loop. Thanks!
Topic archived. No new replies allowed.