【问题标题】:How do i solve overloaded function with no contextual type information error?如何解决没有上下文类型信息错误的重载函数?
【发布时间】:2019-08-16 18:02:34
【问题描述】:

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

然后我有函数 afterElement 插入例如 no。 3之后没有。 7、所以它使用指向find函数的指针作为参数。我认为这就是问题的根源,但我可能错了,你来评判吧。

我想知道,我怎样才能正确使用这个功能?我传递参数的方式有什么问题吗? 我得到的错误是“没有上下文类型信息的重载函数”。

以下是相关代码:

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

【问题讨论】:

  • 只是好奇,为什么将函数指针传递给afterElement() 函数而不是从中调用find() 函数?以及将指针作为函数参数的引用......为什么?
  • afterElement 有一个函数指针作为它的最后一个参数,但是你传递了一个指向节点的指针,即调用find 的结果:(*find)(y,head) 调用函数,如果你想传递函数只需使用&amp;find 作为值参数。

标签: c++ c++11 compiler-errors linked-list arguments


【解决方案1】:

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

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

这是导致您的程序编译的最小修复。 Live demo。请注意,这仅表明编译错误已修复,而不是程序正常运行!

此外,由于您是using namespace std,因此您会收到难以理解的错误消息,因为编译器无法确定您想到的find 是您自己的还是std::find。如果你去掉using namespace std,你的错误信息就会变得更加清晰:

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

Live demo。永远不要使用using namespace std

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

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

会很好用。

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

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

致电afterElement(x, y, find(x, head), head, tail) 使用此变体。注意你不需要说(*find)(x, head)

您的代码存在比这个编译错误更多的问题。例如

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

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

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

【讨论】:

  • 谢谢你,就像一个魅力!也感谢您指出未初始化的节点! :D
【解决方案2】:

我观察到您想将“find”函数作为参数传递给 afterElement 函数。

当然,您可以将一个函数作为参数传递给其他函数。函数也存储在内存位置。该内存位置存储在与函数名称相同的变量中(在本例中为“find”)。

现在您在 afterElement 函数中接收 find 函数参数作为指针,因此它需要一个地址,但您正在传递整个函数。这就是它给出编译错误的原因。 正确的代码如下:

#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); // modified the passing argument
    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;
    }
}
}

我已经检查了编译,但检查一次你所期望的结果。 谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    • 2012-03-07
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多