错误LNK2019:未解决的外部符号
error LNK2019: unresolved external symbol
我最近再次开始在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文件。
相关文章:
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 未解析的外部符号_MsiLocateComponentW@12.
- 在C++中使用 gRPC 时未解析的外部符号
- LibPrivoxy: 未解析的外部符号 __declspec(dllimport) int __stdcall Sta
- 尝试从 XamlApplication 编译 C++/WinRT 空白应用程序时,我收到未解析的外部符号 winrt_make_*
- 我收到错误LNK2001:未解析的外部符号(C++代码)
- 可视抽象类 c++(错误 LNK 2001:未解析的外部符号)
- 如何摆脱C++中未解析的外部符号"private: static char"错误?
- 当 lib 已添加到其他依赖项时,如何在 VS 中调试未解析的外部符号错误
- 仅在少数方法(静态或共享库)中解析的外部符号
- 未解析的外部符号"__declspec(dllimport)与 Spdlog c++ 库
- 函数 _main (OPENGL C++) 中引用的未解析的外部符号 ________
- 错误LNK2019未解析的外部符号"public: __thiscall SLinkList<char>::SLinkList<char>(void)"
- 使用静态 constexpr 成员的未解析外部符号
- 引入参数化构造函数后显示 LNK 2019 未解析外部符号的代码错误
- Dlib LNK2001未解析的外部符号USER_ERROR__consistent_build_configurati
- 矛盾的未解决的外部符号+未使用的库与VS2017和FFMPEG 4
- 链接器错误:切换到unicode生成给出:未解析的外部符号WinMain
- 类模板上一个特定函数的未解析外部符号
- LNK2001:未解析的外部符号public:static类std::vector