2012-04-19 23 views
3

こんにちは私のプログラムでは、 "invader.h"と "game.h"という名前の2つのヘッダファイルをgame.hに持っています。私はinvader.hをインクルードインスタンスに渡すためにポインタを渡したいからです。私はまた、invader.hのgame.hを含めるが、私はerror.ifをコンパイルした私はinvader.hからgame.hを削除してうまく動作します。私はすでに各ヘッダファイルにインクルードガードを追加しています。私が今まで見つけたものに基づいて、invader.hのゲームクラスの前方宣言を追加します。なぜなら、invader.hのゲームインスタンスへのポインタが必要なのですが、invader.cppのゲーム関数を呼び出すときに、それは不完全なクラス型へのポインタが許可されていないと言います。この問題を解決するにはどうすればよいですか?ヘッダーファイルの重複を避けるにはどうすればよいですか?

Game.h

#ifndef GAME_H 
#define GAME_H 
#include "Tank.h" 
#include "Invader.h" 
#include "Block.h" 
#include "Bullet.h" 

class Game 
{ 
private: 
Tank tank; 
Invader invaders[11][5]; 
Block blocks[4]; 
bool logicRequiredThisLoop = false; 
public: 
Game(); 
void initEntities(); 
Tank* getTank(){return &tank;}; 
Invader* getInvaders(){return &invaders[0][0];}; 
Block* getBlocks(){return &blocks[0];}; 
void updateLogic(); 
}; 
#endif 

Invader.h

#ifndef INVADER_H 
#define INVADER_H 
#include "Entity.h" 
class Game; //forward declaration of class Game 
class Invader: public Entity 
{ 
private: 
Game* game; 
public: 
Invader(){}; 
Invader(Game*,char*,int,int,int,int,int,int); 
void move(long delta); 
void doLogic(); 
}; 
#endif 

Invader.cpp

Invader.cppで

私がメンバーとなっている)updateLogicを(呼び出そうGameクラスの関数では、不完全なクラスへのポインタが許可されていないというエラーが発生します。

実際に私がここで知りたい最も基本的なことは、私のコードでGameクラスにはinvader型のメンバ変数があるので、invaderクラスのGameクラスのメンバ関数をどのように呼び出すことができますか? Invader.hをGame.hにインクルードし、Invader.hにGameh.hをインクルードします。コンパイルエラーが発生します。

1>ClCompile: 
1> Invader.cpp 
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(13): error C2146: syntax error : missing ';' before identifier 'invaders' 
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(21): error C2143: syntax error : missing ';' before '*' 
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(21): warning C4183: 'getInvaders': missing return type; assumed to be a member function returning 'int' 
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(21): error C2065: 'invaders' : undeclared identifier 
+0

私たちのコードを表示します。 –

+0

@CarlNorum私はソースコード – Tony

+0

を投稿しました@JoachimPileborg:彼はすでに* "それは不完全なクラスタイプへのポインタは許可されていません" * –

答えて

1

この問題を解決するにはどうすればよいですか?
最初に不完全な型が何を意味するかを理解したよう

What leads to incomplete types?

あなたは何かが間違っているので、あなたは、あなたのデザインを再訪問shoud不完全なタイプであるタイプなしの宣言を転送使用できない場合。

詳細な回答が必要な場合は、ソースコードを入力する必要があります。

EDIT:
あなたはInvader.cppGame.hを含める必要があります。

//Invader.cpp

#include "Invader.h" 
#include "Game.h" 
+0

私はソースコードを投稿します – Tony

+0

@tonyaziten:あなたの編集に答えるように更新しました。 –

+0

私が元々やっていたことですが、エラーが発生したので、私はオンラインで検索しました。だから私はここで前方宣言を使うのです – Tony

0

私は今、私の地元のボックスで、私が持っている二つのファイルを参照してこれを説明します:

これは私がInvader.hでGame.hを含む場合、GETにするものです。

server.hworker.h

server.h:

#ifndef SERVER_H_ 
#define SERVER_H_ 

typedef struct _conf conf; 
typedef struct _worker worker; 
typedef struct _logger logger; 

typedef struct _server server; 
struct _server { 
    /* config */ 
    conf *cfg; 

    /* socket */ 
    int fd; 
    struct event_base *base; 
    struct event *signal; 

    /* workers */ 
    worker **w; 

    /* log */ 
    logger *log; 
}; 
... 
... 
#endif /* SERVER_H_ */ 

worker.h

#ifndef WORKER_H_ 
#define WORKER_H_ 

#include <pthread.h> 

typedef struct _server server; 

typedef struct _worker worker; 
struct _worker { 
    pthread_t t; 

    struct event_base *base; 
    struct evhttp *http; 

    server *s; 
}; 
... 
... 
#endif /* WORKER_H_ */ 

あなたは、両方のサーバーと労働者の構造体が解かれ、相互に参照することが見ることができるように.hファイルの先頭の前方宣言によって:例えばserver.hの上部に

typedef struct _worker worker; 

それが労働者の構造体への参照を作るために十分です。 完全なファイルをチェックして、さらに参考にしたい場合があります。https://github.com/abhinavsingh/pulsar/tree/master/include

+0

OPは、フォワード宣言が何であるかを既に理解しています。 –

+0

それは、以下のAlsが指すコードの設計に問題があります。 –

関連する問題