2013-04-02 20 views
0

我想通過一個函數作爲參數在C++中。我不斷收到一個LNK2019無法解析的外部符號錯誤。錯誤LNK2019「無法解析的外部符號」,當傳遞一個函數作爲參數

我已閱讀過這些內容,並已修復過這些內容,但我無法弄清楚我在這裏做錯了什麼。

main.ccp

的錯誤似乎與我的顯示功能中我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 } 

} 

調試注意事項:

我試圖在主要使用這種類型的,並沒有收到任何錯誤。另外,無論我是否註釋掉該函數的用法,它是否出錯。所以它似乎是我的函數定義的問題,而不是它的用法。

任何幫助表示讚賞。提前致謝!

+0

請包括您收到的** complete **錯誤。你已經省略了告訴你的部分*什麼符號是未解決的* –

+0

對不起,也許我應該更具描述性。我試圖讓事情儘可能簡單。現在解決了,謝謝。 – SomeRandomDeveloper

回答

6
void display(TreeItemType& item){ 

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

現貨的區別?提示,它以字母'c'開頭。

但是,由於該代碼不應該編譯。所以你也必須有其他問題。始終發佈真實的代碼。

+0

.... *提示以'c'開始,以'onst'*結尾* :) +1 –

+0

OIC!現在這一切都有道理。謝謝,我想我只需要額外的眼睛! – SomeRandomDeveloper

0

當我沒有寫出函數定義時,我通常會得到這個錯誤,只有聲明。例如,當我註釋掉實施無效AITank ::受阻()像這樣:

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

,但我已經叫那個函數的地方(並仍然有無效受阻();在AITank.h文件)

我得到:

錯誤LNK2019:無法解析的外部符號 「市民:無效__thiscall AITank ::受阻(無效)」(阻塞@ AITank @@ $$ FQAEXXZ)函數「公共參考:無效__thiscall Game :: play(void)「(?play @ Game @@ $$ FQAEXXZ)

Have看看這一行說的是什麼,看看它是否告訴你你錯過了哪些功能。

相關問題