C++ 错误 LNK2019:未解析的外部符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13318965/
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-27 17:10:53  来源:igfitidea点击:

error LNK2019: unresolved external symbol

c++visual-studiolinkervisual-studio-2012lnk2019

提问by jbenscoter

I've recently started to program in C++ again, and for the purposes of education, I am working on creating a poker game. The weird part is, I keep getting the following error:

我最近又开始用 C++ 编程,出于教育目的,我正在努力创建一个扑克游戏。奇怪的是,我不断收到以下错误:

1>LearningLanguage01.obj : error LNK2019: unresolved external symbol "public: __thiscall PokerGame::Poker::Poker(void)" (??0Poker@PokerGame@@QAE@XZ) referenced in function "void __cdecl `dynamic initializer for 'pokerGame''(void)" (??__EpokerGame@@YAXXZ)
1>LearningLanguage01.obj : error LNK2019: unresolved external symbol "public: __thiscall PokerGame::Poker::~Poker(void)" (??1Poker@PokerGame@@QAE@XZ) referenced in function "void __cdecl `dynamic atexit destructor for 'pokerGame''(void)" (??__FpokerGame@@YAXXZ)
1>LearningLanguage01.obj : error LNK2019: unresolved external symbol "public: void __thiscall PokerGame::Poker::begin(void)" (?begin@Poker@PokerGame@@QAEXXZ) referenced in function _wmain
1>C:\Visual Studio 2012\Projects\LearningLanguage01\Debug\LearningLanguage01.exe : fatal error LNK1120: 3 unresolved externals

I have done some research on the issue, and most point to the constructor and destructor definition in the header and .cpp not matching. I don't see any issues with the header and .cpp.

我对这个问题做了一些研究,大多数都指向头文件和 .cpp 中的构造函数和析构函数定义不匹配。我没有看到标题和 .cpp 有任何问题。

Here is the code for poker.h:

这是 poker.h 的代码:

#pragma once

#include "Deck.h"

using namespace CardDeck;

namespace PokerGame
{
    const int MAX_HAND_SIZE = 5;

    struct HAND
    {
        public:
            CARD cards[MAX_HAND_SIZE];
    };

    class Poker
    {
        public:
            Poker(void);
            ~Poker(void);
            HAND drawHand(int gameMode);
            void begin();
    };
}

And the code in the .cpp:

.cpp 中的代码:

#include "stdafx.h"
#include "Poker.h"

using namespace PokerGame;

const int TEXAS_HOLDEM = 0;
const int FIVE_CARD = 1;

class Poker
{
    private:
        Deck deck;      

    Poker::Poker()
    {
        deck = Deck();
    }

    Poker::~Poker()
    {
    }

    void Poker::begin()
    {
        deck.shuffle();
    }

    //Draws a hand of cards and returns it to the player
    HAND Poker::drawHand(int gameMode)
    {
        HAND hand;

        if(gameMode == TEXAS_HOLDEM)
        {
            for(int i = 0; i < sizeof(hand.cards); i++)
            {
                hand.cards[i] = deck.drawCard();
            }
        }

        return hand;
    }

};

回答by chris

Because of the comment below, I've rewritten what I had before.

由于下面的评论,我已经重写了我之前的内容。

The problem that the linker is complaining about is that you've declared your member functions in Poker, but haven't defined them. How is this? For starters, you're creating a new class and defining separate member functions in it.

链接器抱怨的问题是您已在 中声明了成员函数Poker,但尚未定义它们。这怎么样?首先,您要创建一个新类并在其中定义单独的成员函数。

Your header file Pokerclass exists in the PokerGamenamespace and your cpp file Pokerclass exists in the global namespace. To fix that issue, put them in the same namespace:

您的头文件Poker类存在于PokerGame命名空间中,而您的 cpp 文件Poker类存在于全局命名空间中。要解决该问题,请将它们放在相同的命名空间中:

//cpp file
namespace PokerGame {
    class Poker {
        ...
    };
}

Now that they're in the same namespace, you have another issue. You're defining your member functions inside the class body, but not the first one. The definitions simply can't go in the body of a class named the same way. Get rid of the whole class in the cpp file:

现在它们在同一个命名空间中,你还有另一个问题。您在类体内定义成员函数,但不是第一个。定义根本不能进入以相同方式命名的类的主体。去掉 cpp 文件中的整个类:

//cpp file
namespace PokerGame {
    Poker::Poker() {
        deck = Deck(); //consider a member initializer instead
    }

    //other definitions
}

One last thing: you put the private section of your class in the wrong spot. It was in that cpp file class that we just removed. It belongs with the other parts of your class:

最后一件事:您将班级的私人部分放在了错误的位置。正是在我们刚刚删除的那个 cpp 文件类中。它属于您班级的其他部分:

//header file
namespace PokerGame {
    class Poker {
    public:
        //public stuff

    private: 
        Deck deck; //moved from cpp file
   };
}

回答by qiuqi

Another solution could be: check the cmake file and make sure it (such as in ADD_EXECUTABLE) includes the .cpp file you listed.

另一种解决方案可能是:检查 cmake 文件并确保它(例如在 ADD_EXECUTABLE 中)包含您列出的 .cpp 文件。