stateパターンの状態 -> コンパイルできない

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
dic

stateパターンの状態 -> コンパイルできない

#1

投稿記事 by dic » 15年前

stateパターンを利用しようと思ってる前回
http://www.play21.jp/board/formz.cgi?ac ... &rln=54610
の続きですが
Stage2を追加し、コンパイルしようとしたらコンパイルエラーになってしまって先に進めません

コンパイルの内容です

---------------------------------------------------------------------------------------------
コンパイル中...
main.cpp
C:\Documents and Settings\デスクトップ\state\main.cpp(48) : error C2027: 認識できない型 'Stage2' が使われています。
C:\Documents and Settings\デスクトップ\state\main.cpp(29) : 'Stage2' の宣言を確認してください。
cl.exe の実行エラー
---------------------------------------------------------------------------------------------
以下がソースです
#include <iostream>

class    Connection
{
};

//--------------------------------------------------------
class    State
{
protected:
    void    ChangeState( Connection*, State* );
public:
    virtual    void    A( Connection* );
    virtual    void    B( Connection* );
    virtual    void    C( Connection* );
    virtual    void    D( Connection* );
};
void    State::ChangeState( Connection *t, State *s )
{
    printf( "State::ChangeState\n" );
}
void    State::A( Connection* )
{
    printf( "State::A\n" );
}

//--------------------------------------------------------
class    Stage1;
class    Stage2;

//--------------------------------------------------------
class    Stage1    :    public    State
{
public:
    static    State*    _instance;
    static    State*    Instance() {
        if( _instance == 0 )
            _instance = new Stage1;
        return _instance;
    }
    void    A( Connection* );
    void    B( Connection* );
};
State*    Stage1::_instance = 0;
void    Stage1::A( Connection* t )
{
    printf( "Stage1::A\n" );
    ChangeState( t, Stage2::Instance() );
}
void    Stage1::B( Connection* t ) { }

//--------------------------------------------------------
class    Stage2    :    public    State
{
public:
    static    State*    _instance;
    static    State*    Instance() {
        if( _instance == 0 )
            _instance = new Stage2;
        return _instance;
    }
    void    C( Connection* );
    void    D( Connection* );
};
void    Stage2::C( Connection* t )
{
    printf( "Stage2::C\n" );
    ChangeState( t, Stage1::Instance() );
}
void    Stage2::D( Connection* ) { }


//--------------------------------------------------------
int main()
{
    State    *stage1 = new Stage1;
    Connection    *connection = new Connection;

    stage1->A( connection );

    return 0;
}
---------------------------------------------------------------------------------------------
Stage2をStage1からコンパイルしようできず
また
Stage1からStage2をコンパイルできない状態です
どのようにして、この関係を解決することができるでしょうか?

MNS

Re:stateパターンの状態 -> コンパイルできない

#2

投稿記事 by MNS » 15年前

Stage1::A( Connection* )とStage1::B( Connection* )の定義、つまり、

void Stage1::A( Connection* t )
{
printf( "Stage1::A\n" );
ChangeState( t, Stage2::Instance() );
}
void Stage1::B( Connection* t ) { }

の部分を、Stage2クラスの定義より後方に移せば良いと思います。

dic

Re:stateパターンの状態 -> コンパイルできない

#3

投稿記事 by dic » 15年前

>MNSさん
解決しました
ありがとうございました

閉鎖

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