Learning to program in c ++. There was a strange error when working with functions:

cannot resolve overloaded function 'max' based on conversion to type 'double' 

The code itself:

  #include <iostream> using namespace std; void maxi(double max) { int i; i=0; float y[5]={3,-2,0.9,0.5,1}; max = -999999; for(i=0; i<5; ++i) { if(y[i] > max) { max = y[i]; } } } int main() { cout <<"max value:"<< maxi(max); return 0; } 

When clearing the max parameter, another error is already in the main function.

    1 answer 1

    In this sentence

     cout <<"max value:"<< maxi(max); ^^^ 

    You use the name max , which you did not specify. On the other hand, thanks to the directive

     using namespace std; 

    You enter names from the std space into the global namespace. Apparently the name max , which corresponds to the standard adrhythm std::max in the namespace std also included in the global namespace, and the compiler thinks that in the sentence shown above you use the max function as an argument.

    Declare max variable in main

    Note that in the maxi function, the argument value is not used. It is overwritten in the function.

     void maxi(double max) { int i; i=0; float y[5]={3,-2,0.9,0.5,1}; max = -999999; ^^^^^^^^^^^^^^ 

    So there is no point in declaring a parameter for a function if its value is not used in the function. You could declare a local variable in a function, such as

     void maxi() { int i; i=0; float y[5]={3,-2,0.9,0.5,1}; double max = -999999; ^^^^^^^^^^^^^^^^^^^^^ 

    Also in the offer

     cout <<"max value:"<< maxi(max); 

    You are trying to use the result of the function. Therefore, the function must have a return value other than void . Its definition could be as follows.

     double maxi() { int i; i=0; double y[5]={3,-2,0.9,0.5,1}; ^^^^^^ double max = -999999; //... return max; } 

    There is also no point in declaring the variable i outside the loop, since it is used only in a loop.

    Keep in mind that in C ++ there is already a standard algorithm std::max_element , declared in the header of <algorithm> , which allows you to find the maximum value of a given sequence.