11

私は最近 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

私はこの問題についていくつかの調査を行いましたが、ほとんどの場合、ヘッダーのコンストラクターとデストラクターの定義と .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();
    };
}

そして、.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;
    }

};
4

2 に答える 2

8

以下のコメントのために、私は以前に持っていたものを書き直しました。

リンカーが不平を言っている問題は、メンバー関数を で宣言し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
}

最後に 1 つ: クラスのプライベート セクションを間違った場所に配置します。削除したのは、その cpp ファイル クラスにありました。それはあなたのクラスの他の部分に属しています:

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

    private: 
        Deck deck; //moved from cpp file
   };
}
于 2013-01-06T12:46:58.693 に答える
0

別の解決策として、cmake ファイルを確認し、(ADD_EXECUTABLE などに) リストした .cpp ファイルが含まれていることを確認します。

于 2013-01-30T21:51:20.133 に答える