jbe*_*ter 11 c++ linker lnk2019 visual-studio visual-studio-2012
我最近又开始用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
Run Code Online (Sandbox Code Playgroud)
我已经对这个问题做了一些研究,大多数都指向头文件中的构造函数和析构函数定义,并且.cpp不匹配.我没有看到标题和.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();
};
}
Run Code Online (Sandbox Code Playgroud)
和.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;
}
};
Run Code Online (Sandbox Code Playgroud)
由于下面的评论,我改写了以前的评论.
链接器抱怨的问题是你已经声明了你的成员函数Poker,但还没有定义它们.这怎么样?对于初学者,您正在创建一个新类并在其中定义单独的成员函数.
您的头文件Poker类存在于PokerGame命名空间中,您的cpp文件Poker类存在于全局命名空间中.要解决该问题,请将它们放在同一名称空间中:
//cpp file
namespace PokerGame {
class Poker {
...
};
}
Run Code Online (Sandbox Code Playgroud)
现在他们在同一名称空间中,你有另一个问题.您在类主体中定义了成员函数,但不是第一个.这些定义根本无法进入以相同方式命名的类的主体.摆脱cpp文件中的整个类:
//cpp file
namespace PokerGame {
Poker::Poker() {
deck = Deck(); //consider a member initializer instead
}
//other definitions
}
Run Code Online (Sandbox Code Playgroud)
最后一件事:你把你班级的私人部分放在了错误的位置.我们刚刚删除了那个cpp文件类.它属于您班级的其他部分:
//header file
namespace PokerGame {
class Poker {
public:
//public stuff
private:
Deck deck; //moved from cpp file
};
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
75689 次 |
| 最近记录: |