[英]How do i solve overloaded function with no contextual type information error?
我正在做一个关于双向链表的程序。 我有功能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;
}
}
}
我观察到您想将“ find”函数作为参数传递给afterElement函数。
当然,您可以将一个函数作为参数传递给其他函数。 函数也存储在存储位置中。 该存储位置存储在与函数名称相同的变量中(在本例中为“ find”)。
现在,您将在findElement函数中将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;
}
}
}
我已经检查过编译了,但是请检查一下您期望的结果。 谢谢。
如果要将一个函数作为参数传递给另一个函数,则只需要使用函数名,而不是整个调用表达式即可。
afterElement(x,y,head,tail,find);
这是导致您的程序编译的最小修复。 现场演示 。 注意,这仅表明编译错误已得到解决,而不能证明程序可以正常工作!
此外,因为你using namespace std
,你越来越难以理解的错误消息,因为编译器无法弄清楚find
你心目中,你自己或std::find
。 如果您放弃using namespace std
,则错误消息将变得更加清晰:
error: cannot convert ‘node*’ to ‘node* (*)(int, node*&)’
现场演示 。 永远不要使用using namespace std
。
然而,你可能要考虑删除find
从参数列表afterElement
。 afterElement
无需告知要调用哪个函数来查找元素。
void afterElement(int x,int after,node*& head,node*& tail)
会很好。
int after
将指针传递给节点而不是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
,它没有指向任何地方,因此无法在其上使用->
。
另一个问题是您的程序永远不会修改head
,因此列表没有机会包含任何内容。 也许应该通过添加更多功能(例如beforeElement
)来解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.