0

我正在尝试将函数作为 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!";
}

bst.h

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 中使用这种类型,并且没有收到错误。另外,无论我是否注释掉该函数的用法,它是否都会出错。所以这似乎是我的函数定义的问题,而不是它的用法。

任何帮助表示赞赏。提前致谢!

4

2 回答 2

6
void display(TreeItemType& item){

typedef void (*FunctionType)(const TreeItemType& item);

指出不同?提示,它以字母“c”开头。

但是,鉴于此代码不应编译。所以你一定还有其他问题。始终发布真实代码。

于 2013-04-02T15:11:28.833 回答
0

当我没有写函数定义时,我通常会得到这个错误,只有声明。例如,当我像这样注释掉 void AITank::obstructed() 的实现时:

//void AITank::obstructed()
//{
//    // some code here
//}

但我在某处调用了该函数(并且在 AITank.h 文件中仍然有 void obeded();)

我得到:

错误 LNK2019:在函数“public: void __thiscall Game::play(void)”(?play@游戏@@$$FQAEXXZ)

查看该行的内容,看看它是否告诉您缺少哪个功能。

于 2013-04-02T15:15:01.807 回答