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

发布于 2024-11-01 04:01:43 字数 2646 浏览 4 评论 0原文

问候。

我已经找到了解决方案,但我认为这个问题是特定于个人代码的,因此我在这里发帖。

我就开门见山吧。

我主要有两个对象。

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

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

Greetings.

I have searched for a solution, but I think this problem is personal code specific, hence my posting here.

I'll get straight to the point.

In my main I have two objects.

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

In the computer class, in the header I have the following:

  private:

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

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

At this point I think the problem arises.

The methods called from the class Strategy all require knowledge of the board thus:

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

The problems im getting, unabling them to compile:

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

As a c++ noob, I have absolutely no clue why or how this problem is caused. I think it has to do something with either the instantiation of Strategy in the computer class, or with the parameter given from computer to the strategy in the method calls.

Can anyone explain WHY this error is occuring, I don't quite understand the error at all.
And also how to solve/prevent this?

EDIT*

I just got a request to share the Strategy class:

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

The class defined these methods, I won't post them because they are quite long.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若沐 2024-11-08 04:01:44

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

您尚未向链接器提供这些函数的定义。如果您在 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
}

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

This is a linking error and it has nothing to do with instantiations or parameters.

You haven't provided the linker with the definitions for those functions. If you defined them in Strategy.cpp you need to compile that and add it as an argument to the linker. How you do that depends entirely on what tools you're using to build your program.
If you're using Visual Studio (or any other IDE) it should take care of it automatically once you've added Strategy.cpp to your project.

Or did you perhaps define them like this:

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

instead of like this:

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

The first one doesn't define a Strategy member function, it defines a global function.

终止放荡 2024-11-08 04:01:44

该错误只是说明您已声明但未定义成员函数 Strategy::makeTwoStrategy::counterStrategy::checkTwoInRow< /代码>。您确定您实现了它们(在实际正在编译的源文件中)并且您没有意外地将它们定义为自由函数吗?

The error is simply stating that you have declared, but not defined, the member functions Strategy::makeTwo, Strategy::counter, and Strategy::checkTwoInRow. Are you sure that you implemented them (in a source file that's actually being compiled) and that you didn't accidentally define them as free functions?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文