c++ - 错误 LNK2019 : unresolved external symbol

标签 c++ visual-studio linker visual-studio-2012 lnk2019

我最近又开始用 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

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

这是 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();
    };
}

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

};

最佳答案

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

链接器提示的问题是您已在 Poker 中声明了您的成员函数,但尚未定义它们。这怎么样?对于初学者,您正在创建一个新类并在其中定义单独的成员函数。

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

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

既然它们在同一个命名空间中,您就会遇到另一个问题。您在类主体中定义您的成员函数,而不是第一个。这些定义根本不能放在以相同方式命名的类的主体中。去掉cpp文件中的整个类:

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

    //other definitions
}

最后一件事:您将类(class)的私有(private)部分放在了错误的位置。它位于我们刚刚删除的那个 cpp 文件类中。它属于你类(class)的其他部分:

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

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

关于c++ - 错误 LNK2019 : unresolved external symbol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13318965/

相关文章:

c++ - 分布式应用命令/控制和监控

python - 将 Cython 融合类型转换为 C++ 指针

c++ - cout 语句中使用的条件运算符

linker - 可执行文件找不到动态链接的 mkl 库,但 ldd 可以

c++ - 在C++中使用curl获取一段时间后更改的页面

visual-studio - ReSharper:查找可选参数的用法

c++ - 在 Visual Studio 2017 中使用 Cmake 构建 ssh.dll

asp.net - Visual Studio需要Windows 7中提升的权限

c - 函数在目标文件之一中,但链接器不链接它们

c - 用 C 链接到一个特定的库