我正在尝试将函数作为C ++中的参数传递。我一直收到LNK2019未解决的外部符号错误。
我已经读过这些,并且已经修复了这些,但我无法弄清楚我在这里做错了什么。
我的显示功能中的TreeItemType引用似乎发生了错误。
include "PhoneBook.h"
include "PhoneEntry.h"
include "bst.h"
using namespace std;
using namespace p04NS;
void display(TreeItemType& item);
int main() {
PhoneEntry test = PhoneEntry("first", "last", "618-580-0680");
bst * tree = new bst();
TreeItemType foo = TreeItemType(); // test type reference, no error
tree->insert(test);
tree->inorder(display);
system("pause");
return 0;
}
void display(TreeItemType& item){
cout << "hello worlds!";
}
这里定义了TreeItemType typedef。
namespace p04NS {
typedef PhoneEntry TreeItemType;
typedef string KeyType;
// Pointer to a function that can be used to display the item.
typedef void (*FunctionType)(const TreeItemType& item);
class bst { //some codez }
}
我尝试在main中使用此类型,并且没有收到任何错误。此外,它是否错误我是否注释掉该功能的使用。所以它似乎是我的函数定义的问题,而不是它的用法。
感谢任何帮助。提前谢谢!
答案 0 :(得分:6)
void display(TreeItemType& item){
和
typedef void (*FunctionType)(const TreeItemType& item);
发现差异?提示,它以字母'c'开头。
然而,鉴于此代码不应编译。所以你也必须有其他一些问题。始终发布实际代码。
答案 1 :(得分:0)
当我没有编写函数定义时,我通常会得到这个错误,只有声明。例如,当我注释掉void AITank :: obstructed()的实现时,就像这样:
//void AITank::obstructed()
//{
// // some code here
//}
但我在某个地方调用了这个函数(并且在AITank.h文件中仍然有阻塞();
我明白了:
错误LNK2019:未解析的外部符号“public:void __thiscall AITank :: obstructed(void)”(?obstructed @ AITank @@ $$ FQAEXXZ)在函数“public:void __thiscall Game :: play(void)”中引用( ?玩游戏@ $$ @@ FQAEXXZ)
看一下该行说的内容,看看是否能告诉你哪个功能缺失了。