Stateパターンのコンパイル順番でコンパイルできない

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
dic
記事: 658
登録日時: 15年前
住所: 宮崎県
連絡を取る:

Stateパターンのコンパイル順番でコンパイルできない

#1

投稿記事 by dic » 14年前

自分では解決できないので質問させてください
現在 Stateパターンを参考にし、下のソースを書き、コンパイルするのですが

C:\Documents and Settings\++++++++++\デスクトップ\DxLib_骨組み Stateパターン\main.cpp(134) : error C2653: 'GameStage' : 識別子がクラス名でも名前空間名でもありません。
C:\Documents and Settings\++++++++++\デスクトップ\DxLib_骨組み Stateパターン\main.cpp(136) : error C2653: 'GameStrategy' : 識別子がクラス名でも名前空間名でもありません。
C:\Documents and Settings\+++++++++++\デスクトップ\DxLib_骨組み Stateパターン\main.cpp(161) : error C2653: 'GameStrategy' : 識別子がクラス名でも名前空間名でもありません。

とコンパイルエラーがでてしまいます
コンパイルの順番で、型が定義されていないのはわかっていますが
いまのソースコードの順番を入れ替えても、また同じ型が定義されていないと
コンパイルエラーがおきてしまいそうです

これはどのようにすればコンパイルが通るようになるのでしょうか?

コード:

//==================================================================
class	GameConnection;
class	GameState
{
public:
	virtual	void	Draw( GameConnection *t );
	virtual void	GoTitle( GameConnection *t );
	virtual	void	GoStage( GameConnection *t );
	virtual void	GoStrategy( GameConnection *t );
	virtual	void	ChangeState( GameConnection*, GameState* );
};
class	GameConnection
{
	GameState	*_state;
public:
	GameConnection( GameState * );
	void	UpDate();
	void	ChangeState( GameState *s );
};
GameConnection::GameConnection( GameState *s )
{
	_state = s;
}
void	GameConnection::ChangeState( GameState *s )
{
	_state = s;
}
void	GameConnection::UpDate()
{
	_state->Draw( this );
}
//==================================================================
void	GameState::ChangeState( GameConnection *t, GameState *s )
{
}
void	GameState::GoTitle( GameConnection *t )
{
}
void	GameState::GoStage( GameConnection *t )
{
}
void	GameState::GoStrategy( GameConnection *t )
{
}
void	GameState::Draw( GameConnection *t )
{
}
//==================================================================
class	GameTitle	:	public	GameState
{
	static	GameState	*_instance;
public:
	void	Draw( GameConnection *t );
	void	GoStage( GameConnection *t );
	void	GoStrategy( GameConnection *t );
	static	GameState*	GetInstance() {
		if( _instance == 0 ){
			_instance = new GameTitle();
		}
		return _instance;
	}
};
GameState*	GameTitle::_instance;
void	GameTitle::Draw( GameConnection *t )
{
	DrawString( 0, 100, "GameTitle", GetColor(255,255,2555) );

	static	int	a;
	a++;
	if( a==100 )
		t->ChangeState( GameStage::GetInstance() );   // コンパイルエラー GameStage が認識できない
	if( a==200 )
		t->ChangeState( GameStrategy::GetInstance() );   // コンパイルエラー GameStrategyが認識できない
}
void	GameTitle::GoStage( GameConnection *t )
{
}
void	GameTitle::GoStrategy( GameConnection *t )
{
}
//==================================================================
class	GameStage	:	public	GameState
{
	static	GameState	*_instance;
public:
	void	GoTitle();
	void	GoStrategy( GameConnection *t );
	static	GameState*	GetInstance() {
		if( _instance == 0 ){
			_instance = new GameStage();
		}
		return _instance;
	}
};
GameState*	GameStage::_instance;
void	GameStage::GoStrategy( GameConnection *t )
{
	t->ChangeState( GameStrategy::GetInstance() );
}
//==================================================================
class	GameStrategy	:	public	GameState
{
	static	GameState	*_instance;
public:
	void	GoTitle();
	void	GoStage();
	static	GameState*	GetInstance() {
		if( _instance == 0 ){
			_instance = new GameStrategy();
		}
		return _instance;
	}
};
GameState*	GameStrategy::_instance;



static	GameConnection	*connection;
//==================================================================
void	func2()
{
	static	int	c = 0;
	if( c == 0 )
	{
		connection = new GameConnection( GameTitle::GetInstance() );
		c = 1;
	}
	connection->UpDate();
}

アバター
a5ua
記事: 199
登録日時: 15年前

Re: Stateパターンのコンパイル順番でコンパイルできない

#2

投稿記事 by a5ua » 14年前

GameTitle::Drawの定義は、GameStageクラスやGameStrategyの定義よりも後でないといけません
GameStage::GoStrategyの定義は、GameStrategyの定義よりも後でないといけません。

アドバイスとしては、前方で宣言のみを行なった後に定義を書けば、順番を気にせずにすみます。

// A,B,Cはクラスだったり関数だったり
Aの宣言
Bの宣言
Cの宣言

// 順番を気にしなくてよい
Aの定義
Cの定義
Bの定義

dicさんのコードの場合、

Aの宣言
Bの宣言
Bの定義
Aの定義 { Cを使用 // エラー:Cは宣言されていない }
Cの宣言
Cの定義

のようになっているため、コンパイルできません。


ところで、宣言のみをヘッダファイルにまとめて書き、定義を別のソースファイルに書くのが、
分割コンパイルの基本ですので、そのように書き直してみるといいでしょう。

dic
記事: 658
登録日時: 15年前
住所: 宮崎県
連絡を取る:

Re: Stateパターンのコンパイル順番でコンパイルできない

#3

投稿記事 by dic » 14年前

>>a5uaさん
なるほど、宣言のみ先にして、定義をまとめてあとに書いたらコンパイルが通りました
続いて分割にしようかと思います
ありがとうございました

閉鎖

“C言語何でも質問掲示板” へ戻る