如何解决没有上下文类型信息错误的重载函数? [英] How do i solve overloaded function with no contextual type information error?

查看:149
本文介绍了如何解决没有上下文类型信息错误的重载函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个关于双向链表的程序.我有函数 find 可以帮助定位,如果本身没有.7 在该列表中的任何位置.此函数工作正常并返回指向该节点的指针.

I'm doing a program about doubly linked list. I have function find that helps locate, if per se, no. 7 is anywhere within that list. This function works fine and returns pointer to that node.

然后我有函数 afterElement 插入例如 no.3后没有.7、所以它使用指向find函数的指针作为参数.我认为这是问题的根源,但我可能是错的,你是法官.

Then I have function afterElement that inserts for example no. 3 after no. 7, So it uses pointer to find function as parameter. I think this is where the problem stems from, but I might be wrong, you be the judge.

我想知道,如何正确使用这个功能?我传递参数的方式或其他方式有什么问题吗?我得到的错误是没有上下文类型信息的重载函数".

I wanna know, how can I correctly use this function? Is there something wrong with how I pass parameters or else? The error I get is "overloaded function with no contextual type information".

相关代码如下:

#include <iostream>
using namespace std;

struct node {
int data;
node* prev;
node* next;
};

node* find(int,node*&);
void afterElement(int,int,node*&,node*&,node* (*find)(int, node*&));

int main() {
    node* head = NULL;
    node* tail = NULL;
    // The program itself has a menu that allows for input of value in list but
    // for the sake of relevancy and shortness of code I dropped it out from here

    int x, y;
    cout << "Insert 2 values: value you wish to insert, and value you wish to insert it after. ";
    cin >> x;
    cin >> y;
    afterElement(x,y,head,tail,(*find)(y,head)); // here is the error "overloaded function..."
    return 0;
}

node* find(int x,node*& head) {
    node* curr = head;
    while ((curr != NULL) && (curr->data != x))
        curr = curr->next;
    return curr;
}

void afterElement(int x,int after,node*& head,node*& tail,node* (*find)(int x, node*& head)) {
node* N;
node* compared = (*find)(after,head);
N->data = x;
if (compared == NULL)
    cout << "There is no element " << after << " in the list!\n";
else {
    if (compared->next == NULL) {
        compared->next = N;
        N->prev = compared;
        N->next = NULL;
        tail = N;
    } else {
        compared->next->prev = N;
        N->next = compared->next;
        compared->next = N;
        N->prev = compared;
    }
}
}

推荐答案

如果你想把一个函数作为参数传递给另一个函数,你只需要使用函数名,而不是整个调用表达式.

If you want to pass a function as an argument to another function, you just need to use the function name, not the entire call expression.

afterElement(x,y,head,tail,find); 

这是导致您的程序编译的最小修复.现场演示.请注意,这仅表明编译错误已修复,并不表明程序有效!

This is the minimal fix that causes your program to compile. Live demo. Note this only demonstrates that the compilation errors are fixed, not that the program works!

此外,由于您正在使用命名空间 std,您会收到难以理解的错误消息,因为编译器无法弄清楚您在想什么find,您自己的或std::find.如果你摆脱了 using namespace std,你的错误信息就会变得更加清晰:

In addition, because you are using namespace std, you are getting incomprehensible error messages, since the compiler cannot figure out what find you have in mind, your own or std::find. If you get rid of using namespace std, your error message becomes much clearer:

error: cannot convert ‘node*’ to ‘node* (*)(int, node*&)’

现场演示.永远不要使用 using namespace std.

然而,您可能需要考虑从 afterElement 的参数列表中删除 find.afterElement 不需要被告知要调用哪个函数来查找元素.

However you may want to consider removing find from the parameter list of afterElement. afterElement doesn't need to be told which function to call in order to find an element.

void afterElement(int x,int after,node*& head,node*& tail)

会很好用.

传递一个指向节点的指针而不是 int after 也可以:

Passing a pointer to a node instead of int after will also work:

void afterElement(int x, node* after, node*& head, node*& tail)

调用 afterElement(x, y, find(x, head), head, tail) 来使用这个变体.请注意,您不需要说 (*find)(x, head).

Call afterElement(x, y, find(x, head), head, tail) to use this variant. Note you don't need to say (*find)(x, head).

您的代码有比这个编译错误更多的问题.例如

There are more problems with your code than this compilation error. For example

node* N;
...
N->data = x;

不正确.你还没有初始化 N,它没有指向任何地方,所以你不能在它上面使用 ->.

is incorrect. You have not initialised N, it doesn't point anywhere, so you cannot use -> on it.

另一个问题是您的程序从不修改head,因此列表没有机会包含任何内容.这也许应该通过添加更多函数来解决(可能类似于 beforeElement).

Another problem is that your program never modifies head, so the list doesn't have a chance to contain anything. This perhaps should be fixed by adding more functions (maybe something like beforeElement).

这篇关于如何解决没有上下文类型信息错误的重载函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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