簡體   English   中英

C ++ LNK2001:未解決的外部符號問題

[英]c++ LNK2001: unresolved external symbol problem

問候。

我已經在尋找解決方案,但是我認為這個問題是個人代碼特定的,因此我在這里發布。

我會直截了當。

我主要有兩個對象。

Computer *computer = new Computer();
Player *player = new Player();

在計算機類的標題中,我具有以下內容:

  private:

Strategy *strategy;
int winningPosition;
int twoInRow;
int counter;
int makeTwo;

然后在Computer.cpp中:

Computer::Computer(char token, bool hasTurn)
{
    m_token = token;
    m_hasTurn = hasTurn;
    strategy = new Strategy();
}

int Computer::getMove(const char (&board)[9])
{
    twoInRow = strategy->checkTwoInRow(board);
    counter = strategy->counter(board);
    makeTwo = strategy->makeTwo(board);

    if(twoInRow != 0)
    {
        return twoInRow - 1;
    } else if(counter != 0) {
        return counter - 1;
    } else if(makeTwo != 0) {
        return makeTwo - 1;
    } else {
        return 0;
    }
}

在這一點上,我認為出現了問題。

從策略類中調用的方法都需要董事會知識,因此:

int checkTwoInRow(const char (&board)[9]);
int counter(const char (&board)[9]);
int makeTwo(const char (&board)[9]);

問題越來越嚴重,無法編譯:

Error   1   error LNK2019: unresolved external symbol "public: int __thiscall Strategy::makeTwo(char const (&)[9])" (?makeTwo@Strategy@@QAEHAAY08$$CBD@Z) referenced in function "public: int __thiscall Computer::getMove(char const (&)[9])" (?getMove@Computer@@QAEHAAY08$$CBD@Z)    C:\CPP\TTT\Computer.obj tictactoeCPP

Error   2   error LNK2019: unresolved external symbol "public: int __thiscall Strategy::counter(char const (&)[9])" (?counter@Strategy@@QAEHAAY08$$CBD@Z) referenced in function "public: int __thiscall Computer::getMove(char const (&)[9])" (?getMove@Computer@@QAEHAAY08$$CBD@Z)    C:\CPP\TTT\Computer.obj tictactoeCPP

Error   3   error LNK2019: unresolved external symbol "public: int __thiscall Strategy::checkTwoInRow(char const (&)[9])" (?checkTwoInRow@Strategy@@QAEHAAY08$$CBD@Z) referenced in function "public: int __thiscall Computer::getMove(char const (&)[9])" (?getMove@Computer@@QAEHAAY08$$CBD@Z)    C:\CPP\TTT\Computer.obj tictactoeCPP

作為c ++新手,我完全不知道為什么或如何導致此問題。 我認為它與計算機類中Strategy的實例化或方法調用中從計算機給該策略提供的參數有關。

誰能解釋為什么會發生此錯誤,但我完全不了解該錯誤。 以及如何解決/防止這種情況?

編輯*

我剛剛收到一個分享“策略”課程的請求:

Strategy.h:

    #pragma once
class Strategy
{
public:
    Strategy(void);
    ~Strategy(void);

    int checkTwoInRow(const char (&board)[9]);
    int counter(const char (&board)[9]);
    int makeTwo(const char (&board)[9]);
};

該類定義了這些方法,因為它們很長,所以我不會發布它們。

這是一個鏈接錯誤,與實例化或參數無關。

您尚未為鏈接器提供這些功能的定義。 如果在Strategy.cpp中定義了它們,則需要對其進行編譯並將其作為自變量添加到鏈接器。 如何執行完全取決於您用來構建程序的工具。
如果您使用的是Visual Studio(或任何其他IDE),則應在將Strategy.cpp添加到項目后自動對其進行處理。

還是您可能這樣定義它們:

int checkTwoInRow(const char (&board)[9])
{
   // Do something with board the wrong way
}

而不是像這樣:

int Strategy::checkTwoInRow(const char (&board)[9])
{
   // Do something with board the right way
}

第一個沒有定義Strategy成員函數,而是定義了一個全局函數。

該錯誤僅表示您已聲明但尚未定義成員函數Strategy::makeTwoStrategy::counterStrategy::checkTwoInRow 您確定已實現它們(在實際正在編譯的源文件中)並且沒有意外將它們定義為自由函數嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM