1

ご挨拶。

解決策を探しましたが、この問題は個人のコードに固有のものだと思うため、ここに投稿します。

いきなり本題に入ります。

私のメインでは、2 つのオブジェクトがあります。

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;
    }
}

この時点で、問題が発生すると思います。

クラス Strategy から呼び出されるメソッドはすべて、次のようにボードの知識を必要とします。

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 クラスを共有したいというリクエストを受け取りました。

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]);
};

クラスはこれらのメソッドを定義しましたが、かなり長いので投稿しません。

4

2 に答える 2

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 メンバー関数を定義せず、グローバル関数を定義します。

于 2011-04-13T11:34:49.570 に答える
3

Strategy::makeTwoこのエラーは、メンバー関数、Strategy::counter、およびを宣言したが、定義していないことを示しているだけですStrategy::checkTwoInRow。それらを (実際にコンパイルされているソース ファイルに) 実装したこと、およびそれらを誤ってフリー関数として定義していないことは確かですか?

于 2011-04-13T10:08:16.807 に答える