没有与班级成员匹配的电话 [英] No matching call for class member

查看:68
本文介绍了没有与班级成员匹配的电话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为各种程序尝试此项目,以进行列表,搜索等.因此,我打算给它最后一次尝试来解决我遇到的错误.我已经实现了一个通用列表,并且试图从列表中的某个位置检索数据.
但我遇到一个错误:没有匹配的函数可用于调用"List :: retrieve(int& ;, Record&)"
以下是main.cpp的代码以及从List.h
检索的功能代码段 main.cpp

I have been trying this project for various programs now for list, search, etc. So, I intend to give it one last try to resolve the errors I am getting. I have implemented a generic list and I am trying to retrieve the data from a certain position in the list.
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
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



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;
    }



显然,List_entry是int类型的,而Record不是错误的记录.由于Record是包装int的Key的别名,因此我将检索中的第二个参数更改为the_list.retrieve(p,data.the_key());.但它仍然给出了错误.
接下来,我将List< int >更改为List < Key >,但仍然存在错误.

完整代码:
Main.cpp: http://pastebin.com/UrBPzPvi [ ^ ]
List.h: http://pastebin.com/7tcbSuQu [ ^ ]
Key.h http://pastebin.com/DrAH0MPq [ ^ ]
Key.cpp http://pastebin.com/xjTvgxxA [ ^ ]



Obviously, the List_entry is of type int and Record is not as from the error. Since Record is an alias of Key which wraps int, so I changed the second parameter in retrieve as this the_list.retrieve(p, data.the_key()); but it still gave the error.
Next, I changed List< int > to List < Key >but still the error exists.

For full code:
Main.cpp: http://pastebin.com/UrBPzPvi[^]
List.h: http://pastebin.com/7tcbSuQu[^]
Key.h http://pastebin.com/DrAH0MPq[^]
Key.cpp http://pastebin.com/xjTvgxxA[^]

推荐答案

再次?哦,我明白了:这次问题有点不同:

在函数main中,声明一个List的实例:

Again? Oh, I see: this time the problem is a bit different:

In the function main, you declare an instance of List:

List<int> the_list;



必须为:



Must be:

List<Record> the_list;



您正在尝试使用List<List_entry>,其中List_entryRecord,因此需要相应地声明类型.



You''re trying to use List<List_entry> where List_entry is Record, so you need to declare the type accordingly.


这是另一个错误:

This is another bug:

cout<<"Record value: "<<data;



它无法编译,因为您试图使用类型为Record的变量data调用中缀流输出运算符"<<",但是您从未为该类型定义此运算符(流运算符如何知道您是指这种类型的文本输出吗?).

您不必定义运算符.您可以简单地输出Record的单独成员,而不是整个Record:



It does not compile because you''re trying to call infix stream output operator "<<" with variable data of the type Record, but you never defined this operator for this type (how stream operator would know what do you mean by text output for this type?).

You don''t have to define the operator. You can simply output separate member(s) of Record instead of whole Record:

cout<<"Record value: "<<data.the_key();



这样的事情.

现在好些了吗?



Something like that.

Better now?


我的答案有个额外的建议(对不起,我重复一遍,但是这次我会说得更清楚).

您确实非常需要摆脱Error_code.方法如下:

A bonus advice to my answer (sorry I repeat myself, but this time I''ll make in more clear).

You really, really need to get rid of Error_code. Here is how:

List_entry retrieve(int position) const
{
    if (empty()) throw "underflow";
    if (position<0 || position>count) throw "range error";
    return entry[position];
}



仅作为示例,此throw部分得到了简化.代替字符串,最好是某些类或针对不同错误的不同类.对于范围错误,应使用有效范围和position的实际值进行初始化.

异常的好处如此之多,以至于涉及返回错误信息的技术已不再有意义,因为结构异常处理是图灵奖获得者Barbara Liskov于2008年发明的(对于CLU语言,1970年左右). />
不幸的是,现在没有时间解释所有技术和动机,但是关于该主题有大量文献可供选择. (我不熟悉入门文献;当您尚未在C ++中引入该技术时,我便是该技术的早期实现者之一-信不信由你.)



This throw part is simplified, just for example. Instead of string, it should better be some class or different classes for different errors; for range error it should be initialized with valid range and actual value of position.

The benefits of exceptions are so overwhelming, that the techniques which involve returning of error information makes no sense anymore, since structural exception handling was invented (for CLU language, circa 1970) by Barbara Liskov, a Turing Award laureate (2008).

Unfortunately, this is no time to explain all the techniques and the motivation, but there is a huge choice of literature on the topic. (I am not familiar with the introductory literature; I am one of the early implementors of the technology when it was not yet introduced in C++ -- believe or not.)


这篇关于没有与班级成员匹配的电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆