c ++ LNK2001:未解决的外部符号问题

Joe*_*ing 1 c++ lnk

问候.

我已经搜索了一个解决方案,但我认为这个问题是个人代码特定的,因此我在这里发布.

我会直言不讳.

在我的主要我有两个对象.

Computer *computer = new Computer();
Player *player = new Player();
Run Code Online (Sandbox Code Playgroud)

在计算机类中,在标题中我有以下内容:

  private:

Strategy *strategy;
int winningPosition;
int twoInRow;
int counter;
int makeTwo;
Run Code Online (Sandbox Code Playgroud)

然后在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;
    }
}
Run Code Online (Sandbox Code Playgroud)

在这一点上,我认为问题出现了.

从战略类中调用的方法都需要董事会的知识,因此:

int checkTwoInRow(const char (&board)[9]);
int counter(const char (&board)[9]);
int makeTwo(const char (&board)[9]);
Run Code Online (Sandbox Code Playgroud)

我得到的问题,使他们无法编译:

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
Run Code Online (Sandbox Code Playgroud)

作为一个c ++ noob,我完全不知道为什么或如何导致这个问题.我认为它必须对计算机类中的策略实例化,或者从计算机给出的方法调用策略中的参数做一些事情.

任何人都可以解释为什么会出现这个错误,我根本不理解这个错误.还有如何解决/防止这种情况?

编辑*

我刚收到分享策略类的请求:

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]);
};
Run Code Online (Sandbox Code Playgroud)

该类定义了这些方法,我不会发布它们,因为它们很长.

mol*_*ilo 8

这是一个链接错误,它与实例化或参数无关.

您尚未向链接器提供这些函数的定义.如果您在Strategy.cpp中定义它们,则需要对其进行编译并将其作为参数添加到链接器中.如何做到这完全取决于您用于构建程序的工具.
如果您正在使用Visual Studio(或任何其他IDE),一旦您将Strategy.cpp添加到项目中,它应该自动处理它.

或者您可能像这样定义它们:

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

而不是像这样:

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

第一个没有定义策略成员函数,它定义了一个全局函数.