错误LNK2019:未解决的外部符号

error LNK2019: unresolved external symbol

本文关键字:外部 符号 未解决 LNK2019 错误      更新时间:2023-10-16

我最近再次开始在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 2012ProjectsLearningLanguage01DebugLearningLanguage01.exe : fatal error LNK1120: 3 unresolved externals

我已经对这个问题进行了一些研究,并且大多数指向标题和.cpp中的构造函数和驱动器定义不匹配。我看不到标题和.cpp。

的任何问题

这是扑克的代码:

#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
}

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

//header file
namespace PokerGame {
    class Poker {
    public:
        //public stuff
    private: 
        Deck deck; //moved from cpp file
   };
}

另一个解决方案可以是:检查cmake文件并确保(例如add_executable中的)包含您列出的.cpp文件。