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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5647456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:36:17  来源:igfitidea点击:

c++ LNK2001: unresolved external symbol problem

c++lnk

提问by Joey Roosing

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.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:

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

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.

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

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 课程的请求:

Strategy.h:

策略.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.

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

回答by molbdnilo

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.

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

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.

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

回答by ildjarn

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?

该错误只是说明你已经声明,但没有定义,成员函数Strategy::makeTwoStrategy::counter以及Strategy::checkTwoInRow。您确定您实现了它们(在实际编译的源文件中)并且您没有意外地将它们定义为自由函数?