C++ 对类成员没有匹配的函数调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4581803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:49:37  来源:igfitidea点击:

No matching function call to class member

c++

提问by Cipher

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

我已经实现了一个通用列表,我正在尝试从列表中的某个位置检索数据。嗯...但我收到一个错误:没有匹配的函数调用'List::retrieve(int&, Record&)' 下面是main.cpp的代码和从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

列表.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

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

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

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.

PS我只是在学习基础知识,对于大规模可重用模块,代码可能并不完美。在这个阶段,它只需要工作。

Thanks

谢谢

回答by James McNellis

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

data,您尝试将其作为第二个参数传递给retrieve,其类型为Record

The second parameter of retrieveis of type List_entry, not Record.

的第二个参数retrieve是 类型List_entry,不是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.

当编译器说“没有匹配的函数”时,这通常意味着它找到了一个与您使用的名称相同的函数,但您尝试传递给该函数的一个或多个参数类型错误,或者您正试图传递函数的参数数量错误。

回答by templatetypedef

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.

错误“没有匹配的调用函数 [...]”通常意味着“我找不到您可以使用以下参数调用的函数。” 它可能意味着很多事情 - 要么你拼错了函数名,要么参数类型错误,要么你试图在 const 对象上调用非常量成员函数,等等。通常,错误会给你一些有关究竟出了什么问题的更多详细信息,包括它尝试匹配的函数,以及在调用站点实际找到的参数类型。模板可能会使这更难阅读,但只要花一点时间,您通常就可以学会阅读它们。

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.

关于这段代码,检索函数的第二个参数是 List_entry 类型,它是 List 的模板参数。在您的主函数中,您实例化了一个 List,因此在这种情况下 List_entry 是一个 int。但是,您正在尝试查找 Record,它(我假设)不是 int。如果您更改代码以尝试查找 int,或将 List 设为 List,则此问题应该会消失。

Hope this helps!

希望这可以帮助!