Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No matching function call to class member

Tags:

c++

I have implemented a generic list and I am trying to retrieve the data from a certain position in the list. umm... but I am getting an error: no matching function for call to 'List::retrieve(int&, Record&)' Below is the code of main.cpp and a snippet of function retrieve from List.h.#include

Main.cpp

#include <iostream>
#include "List.h"    
#include "Key.h"
using namespace std;
typedef Key Record;
int main()
{
    int n;
    int p=3;
    List<int> the_list;
    Record data;
    cout<<"Enter the number of records to be stored. "<<endl;
    cin>>n;
    for(int i=0;i<n;i=i++)
    {
    the_list.insert(i,i);
    }
    cout<<the_list.size();
    the_list.retrieve(p, data);
    cout<<"Record value: "<<data;
    return 0;
}

List.h

Error_code retrieve(int position, List_entry &x)const
    {
    if(empty()) return underflow;
    if(position<0 || position>count) return range_error;
    x=entry[position];
    return success;
    }

For full code:

Main.cpp: http://pastebin.com/UrBPzPvi

List.h: http://pastebin.com/7tcbSuQu

P.S I am just learning the basics and the code may not be perfect with regards to large scale reusable module. At this stage, it just needs to work.

Thanks

like image 976
Cipher Avatar asked Jan 03 '11 02:01

Cipher


2 Answers

data, which you are trying to pass as the second argument to retrieve, is of type Record.

The second parameter of retrieve is of type List_entry, not Record.

When the compiler says "no matching function," that usually means that it found a function with the name you used but one or more of the arguments that you are trying to pass to that function are of the wrong type or you are trying to pass the wrong number of arguments to the function.

like image 133
James McNellis Avatar answered Sep 30 '22 13:09

James McNellis


The error "No matching function for call [...]" usually means "I can't find a function that you can call with the following arguments." It could mean many things - either you misspelled the function name, or the arguments are of the wrong type, or you're trying to call a non-const member function on a const object, etc. Usually, the error will give you some more details about exactly what went wrong, including the functions it tried matching against, along with the types of the arguments that were actually found at the call site. Templates can make this harder to read, but with a bit of time you can usually learn to read them.

In regards to this code, the retrieve function's second argument is of type List_entry, which is the template parameter to List. In your main function, you instantiate a List, so List_entry is an int in this case. However, you're trying to look up a Record, which (I assume) isn't an int. If you either change the code to try looking up an int, or make your List a List, this problem should go away.

Hope this helps!

like image 34
templatetypedef Avatar answered Sep 30 '22 13:09

templatetypedef