1

I have a problem with findMax function using recursive find max element in array (code C++):

void findMax (int& imax, int n, int* arr){
    imax=arr[0]?arr[0]:0;
    if(n > 0){
        imax = std::max(arr[n],findMax(imax, n-1, arr)); // error here: No matching function for call to max???
    }
}

can you explains why and solutions for me with this error?

Thanks,

3
  • Because findMax returns nothing. Commented May 4, 2016 at 13:53
  • findmax needs to return an int the larger value Commented May 4, 2016 at 13:56
  • @ songyuanyao: can you help me with solutions? I want to result of findMax will assign to imax varible. Commented May 4, 2016 at 13:57

2 Answers 2

3

std::max will return the greater of the two values, but the return type for findMax is void.

void findMax (int& imax, int n, int* arr)

So it is looking for

std::max(int, void)

which obviously does not exist.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank u so much, now I see where are my problem :)
2

Instead of changing your function to return an int, you could just do a findmax when the range is valid then only call std::max after with that returned value.

void findMax (int& imax, int n, int* arr){
    imax=arr[0]?arr[0]:0;
    if(n > 0){
        findMax(imax, n-1, arr);
    }
    imax = std::max(arr[n], imax);
}

@CoryKramer is correct though, the error is indeed not passing an int to max in your example. If you just wanted to fix that, you could use this alternate solution:

int findMax (int& imax, int n, int* arr){
    imax=arr[0]?arr[0]:0;
    if(n > 0){
        imax = std::max(arr[n],findMax(imax, n-1, arr));
    }
    return imax;
}

1 Comment

Thank U with your solutions, I see my problem :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.