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

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

问题描述

我正在做一个关于双向链表的程序.我有一个 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 .如果您使用命名空间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*&)’

实时演示.切勿使用使用命名空间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之后传递也将起作用:

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天全站免登陆