2011-12-21 3 views
1

Engineという名前のクラスがあります。 TextureManagerクラスのオブジェクトは、クラスEngineの内部およびWindowクラスのオブジェクトです。2つのクラスのスコープに関するエラー

私の問題は、TextureManagerの内部の関数は、クラスウィンドウ内のパブリック関数または変数にアクセスできないということです。

これはC++の意図した機能ですか、私はこれらのオブジェクトの一方または両方を間違って宣言/定義していると思いますか?

ご覧のとおり、エンジン内部にはTextureManagerとウィンドウがあります。ここで

class Engine 
{ 
    public: 
     Engine(); 
     ~Engine(); 


     Window gameWindow; 
     TextureManager textures; 

     SDL_Event event; 

     int test; 
}; 

はクラスウィンドウのヘッダー

class Window 
{ 
    public: 
     //Constructors 
     Window(); 
     ~Window(); 

     //Getter Functions 
     SDL_Surface * get_screen(); 

     //Other Functions 
     void toggle_fullscreen(); 
     void render(); 
     void initialize(int screenwidth, int screenheight); 
     bool handle_events(SDL_Event event); 
     bool check_error(); 

    private: 
     bool windowed; 
     bool windowFailure; 

     int screenWidth; 
     int screenHeight; 

     SDL_Surface * screen; 

}; 

ここでは、クラスTextureManager

class TextureManager 
{ 
    public: 
     TextureManager(); 
     ~TextureManager(); 

     int new_texture(std::string filename); 
     int new_section(int indexOfTexture, int x, int y, int width, int height); 

     void render_texture(int textureID, int x, int y); 
     void render_section(int textureSectionID, int x, int y); 
    private: 
     std::vector<Texture> textureIDs; 
     std::vector<TextureSection> textureSectionIDs; 
}; 

のヘッダファイルであり、これは、私はとのトラブルを抱えている機能です。関数呼び出しSDL_BlitSurface()

void TextureManager::render_texture(int textureID, int x, int y) 
{ 
    SDL_Rect coordinates; 
    coordinates.x = x; 
    coordinates.y = y; 
    SDL_BlitSurface(textureIDs[textureID].get_texture(), NULL, gameWindow.get_screen(), &coordinates); 
} 
+0

あなたが必要があるかもしれません:あなたはTextureManagerがgameWindowと対話できるようにするには、次のアプローチを考慮することができます**非常に短い**ビットのコードを投稿する。 – drdwilcox

+1

はい、私はあなたのコードの記述が単なる例を投稿するよりも簡単であると仮定して間違ってしまったと思います! –

+0

十分な公正な、いくつかのソースを今すぐ投稿 – Alidaco

答えて

2

既に述べたように、TextureManagerインスタンスはWindowクラスのgameWindowインスタンスについては知らない。

class TextureManager { 
public: 
    TextureManager(Window& w) : m_refWindow(w) { /*...*/ } 
    /*...*/ 
protected: 
    Window& m_refWindow; 
    /*...*/ 
} 

void TextureManager::SomeMethod() 
{ 
    m_refWindow.DoSomething(); 
} 

Engine::Engine() : 
    gameWindow(), 
    textures(gameWindow) 
{ 
/*...*/ 
} 

texturesオブジェクトはgameWindowオブジェクトにバインドされ、その公共functions.`呼び出すことができるようになります。この方法で

+0

+1質問者が同じ問題に遭遇したときに解決した解決策です。 – twsaef

1

TextureManager は、それがEngineのメンバ変数として使われている知らないでgameWindowにアクセスするとき、それはgameWindowが何であるかわかりませんので、私は、エラーが発生しています。

gameWindowがまったくないコンテキストを含め、TextureManagerをどこでもインスタンス化できるという事実を考慮してください。メンバ関数は、ローカル変数、グローバル変数、またはそれ自身のメンバ変数のみを見ることができます。

+0

ああ、それは意味をなさない。だから私はそれについて何をすべきだと思いますか? – Alidaco

関連する問題