"No matching function to call"

I keep getting an error that says "No matching function to call"
I've compared it to all my other functions that I have written but it still isnt working.

I'm a beginner! I'm trying to write something that asks for a list and returns true if the list is in chronological order.

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
36
37
38
39
40
41
42
43
  
#include <iostream>
using namespace std;

bool isSorted (int list[80], const int size, bool result);

int main()
{
    
    
    int list[80];
    int size = 80;
    bool result;
    
    cout << "Enter list : " << endl;
    
    
    
    for (int i = 0; i < size; i++)
        cin >> list[i];
    
    result = isSorted (list[80], size, result); // I get the error here
    
    if(result == true)
        cout << "List is sorted. " << endl;
    
    else cout << "List is not sorted. " << endl;
    
    return 0;
}

bool isSorted(int l[80], const int s, bool r)

{
    for (int i = 0; i < s; i++)
        
        if (l[i] < l[i+1])
            return true;
    
        else
            return false;
    
}
Last edited on
result = isSorted (list, size, result);

instead of

result = isSorted (list[80], size, result);
The reasoning for Amid's answer:
list[80] this is passing the value of list at index(offset) of 80 which is out of range instead of an array (list).
Thanks!!

Alright, now I have fixed that & only that, but now I get an error "Control may reach end of non-void function". I don't understand what that means or how to fix that.
From what I can see online, the problem for this error is usually that I'm missing a return but I have both there!

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

bool isSorted(int l[80], const int s, bool r)

{
    for (int i = 0; i < s; i++)
    {
        if (l[i] < l[i+1])
            return true;
    
        else
            return false;
    }
}    // gives error here
If s is 0, then the for loop will not run, meaning that neither of the two return statements will be executed. This, you get to the end of the function without a return statement.

Also, I think your isSorted function is implemented wrong - it returns as soon as it checks the first value, the rest of the values are never checked.
As LB mentioned it returns after the first element. What you should do is something like
1
2
3
4
5
bool sorted = true;
iterate over elements while sorted == true and it is within bounds
    if it's not sorted..
        sorted = false;
return sorted; 
@giblit: return as soon as you know it's not sorted, but keep going if you're not sure.
Some people prefer to not have multiple return statements to make the code more readable.
I don't think readability has anything to do with it, it's more of an old superstition from C where there is no RAII.
Giblit, I'm confused on what I need to do and where.
Lets look at what you have right now:

1
2
3
4
5
6
7
8
9
10
11
12
bool isSorted(int l[80], const int s, bool r)

{
    for (int i = 0; i < s; i++)
    {
        if (l[i] < l[i+1])
            return true;
    
        else
            return false;
    }
}


If s is 0 then the for loop will never run which would result in 0 return statements. Also you have it return after the first element. What you should do is return false on the first non-sorted value found and after the entire loop if all of them are sorted (it didn't return false) then you should return true.

Last edited on
I think I switched the true/falses. Now I don't know how to fix the zero part to get rid of the error.
1
2
3
4
5
6
7
8
9

 for (int i = 0; i < s; i++)
    {
        if (l[i] > l[i+1])
            return false;
        
        else
            return true;
Try walking through the function yourself on some paper. Write down the values that you are passing and see what happens when you run it. Either that, or step through it with a debugger.
You are still returning true/false after the first value. Get rid of the else.
Topic archived. No new replies allowed.