ご挨拶。
解決策を探しましたが、この問題は個人のコードに固有のものだと思うため、ここに投稿します。
いきなり本題に入ります。
私のメインでは、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]);
};
クラスはこれらのメソッドを定義しましたが、かなり長いので投稿しません。