2016-12-02 8 views
0

循環参照参照エラーが発生しました。ここでは、CardFactoryでDeck型のオブジェクトを使用し、DeckにCardFactory型のオブジェクトを使用します。どのようにこの問題を解決するためのヒント?C++ヘッダーファイルで循環参照を解決する

//CardFactory.h 
#ifndef CARDFACTORY_H 
#define CARDFACTORY_H 

#include "Deck.h" 

#include <string> 

using std::string; 

class CardFactory { 

public: 
    Deck getDeck(); 
    static CardFactory* getFactory() { 
     static CardFactory singleton; 
     return &singleton; 
    } 

}; 

#endif 

//Deck.h 
#ifndef DECK_H 
#define DECK_H 

#include <vector> 
#include <iostream> 
#include "CardFactory.h" 
using std::ostream; 

class Deck : public std::vector<Card*> { 
    friend ostream& operator<<(ostream& os, const Deck& dt); 
    Card* draw(); 
    Deck(CardFactory* cf); 
}; 

#endif 
+0

へのポインタを使用しているDeckクラスで動作するはずです。 –

答えて

2

前方参照(または前方宣言)。

Deck.hでは、代わりに#include "CardFactory.h"を指定する必要はありません。代わりにクラスを宣言してください。

class CardFactory; 

これは、あなただけのあなたは、ベクターおよび他のSTLコンテナから継承するべきではないクラスCardFactory

+0

「前方宣言」とも呼ばれる –

+0

@MichaelAlbersそれは本当です – artm