2011-12-22 72 views
0

我有以下層級構造:C++使用不正確的參數類型來構造對象

我有以下層次結構:

GameStateBaseClass -> IGameStateInterface -> IntroState 

我遇到的問題是,當我實例化一個使用GameEngine引用的IntroState(如我在GameStateBaseClass中定義的)我得到以下錯誤:

Error 1 error C2664: 'IntroState::IntroState(const IntroState &)' : cannot convert parameter 1 from 'GameEngine' to 'const Short::IntroState &'

在GameStateBaseClass中,我定義了一個構造函數,它接受一個const GameState引用,並在main.cpp中傳入一個遊戲引擎的實例。爲什麼地球上它試圖將我的GameEngine參數轉換爲IntroState引用?

下面是相應的代碼:

GameStateBaseClass.hpp

class GameStateBaseClass 
{ 
    public: 
     GameStateBaseClass(const GameEngine &instance); 
    private: 
     GameStateBaseClass(void); // = delete; // c++1x 
     GameStateBaseClass(const GameStateBaseClass &instance); // = delete; // c++1x 
     GameStateBaseClass operator=(const GameStateBaseClass &instance); // = delete; // c++1x 

     // private members 
     const GameEngine &game_engine_instance; 
} 

GameStateBaseClass.cpp

GameStateBaseClass::GameStateBaseClass(const GameEngine &instance) 
    : game_engine_instance(instance) { 
} 

// IGameStateInterface.hpp 
class IGameStateInterface : GameStateBaseClass 
{ 
    public: 
     virtual void Init() = 0; 
     virtual void Cleanup() = 0; 
     ... // other virtual void methods... 
} 

IntroState.hpp

class IntroState : public IGameStateInterface 
{ 
    virtual void Init() {} 

    virtual void Cleanup() { } 

    // other empty bodies 

} 

這裏的比賽演義NE .HPP文件, GameEngine.hpp

// forward declaration 
class IGameStateInterface; 

class GameEngine 
{ 
    public: 
     void Init(); 
     void CLeanup(); 

     void SetState(IGameStateInterface *state); 
     void AddState(IGameStateInterface *state); 

     // ... other methods for the engine 
}; 

在我main.cpp中,我有以下幾點:

int main(...) { 

GameEngine engine_intance; 

// instantiate the engine 
engine_instance.Init(); 

// load the intro state 
engine_instance.AddState(new IntroState(engine_instance)); 

// main loop 
.... 

return 0; 
} 

我想它只是利用我在GameStateBaseClass定義構造函數中,一個這需要一個const GameEngine引用來構造IntroState,而不是它想要在錯誤消息中轉換的那個。

任何想法?

我遇到的問題是,當我實例使用GameEngine參考IntroState(正如我在GameStateBaseClass定義),我收到以下錯誤:

Error 1 error C2664: 'IntroState::IntroState(const IntroState &)' : cannot convert parameter 1 from 'GameEngine' to 'const Short::IntroState &'

在GameStateBaseClass我定義一個構造函數一個const遊戲狀態參考,並在main.cpp中傳遞一個遊戲引擎的實例。爲什麼地球上它試圖將我的GameEngine參數轉換爲IntroState引用?

下面是相應的代碼:

GameStateBaseClass.hpp

class GameStateBaseClass 
{ 
    public: 
     GameStateBaseClass(const GameEngine &instance); 
    private: 
     GameStateBaseClass(void); // = delete; // c++1x 
     GameStateBaseClass(const GameStateBaseClass &instance); // = delete; // c++1x 
     GameStateBaseClass operator=(const GameStateBaseClass &instance); // = delete; // c++1x 

     // private members 
     const GameEngine &game_engine_instance; 
} 

GameStateBaseClass.cpp

GameStateBaseClass::GameStateBaseClass(const GameEngine &instance) 
    : game_engine_instance(instance) { 
} 

// IGameStateInterface.hpp 
class IGameStateInterface : GameStateBaseClass 
{ 
    public: 
     virtual void Init() = 0; 
     virtual void Cleanup() = 0; 
     ... // other virtual void methods... 
} 

IntroState.hpp

class IntroState : public IGameStateInterface 
{ 
    virtual void Init() {} 

    virtual void Cleanup() { } 

    // other empty bodies 

} 

這裏的比賽演義ne .hpp文件, GameEngine。HPP

// forward declaration 
class IGameStateInterface; 

class GameEngine 
{ 
    public: 
     void Init(); 
     void CLeanup(); 

     void SetState(IGameStateInterface *state); 
     void AddState(IGameStateInterface *state); 

     // ... other methods for the engine 
}; 

在我main.cpp中,我有以下幾點:

int main(...) { 

GameEngine engine_intance; 

// instantiate the engine 
engine_instance.Init(); 

// load the intro state 
engine_instance.AddState(new IntroState(engine_instance)); 

// main loop 
.... 

return 0; 
} 

我想它只是利用我在GameStateBaseClass定義構造函數中,即需要一個const GameEngine參考構造IntroState,而不是它想要在錯誤消息中轉換的那個。

任何想法?

+1

你沒有'IntroState :: IntroState(GameEngine&)'或類似的。那麼'新的IntroState(engine_instance)'應該如何工作? – 2011-12-22 08:53:29

+1

你的問題有點混亂,包括10個不同的代碼片段。你應該嘗試將問題提取到一個最小的場景,否則可能需要在必要時發佈源代碼(最好是可編譯的),如下所示:https://gist.github.com/1509689 – gilligan 2011-12-22 09:39:26

回答

6

IntroState類沒有構造,可以採取型GameEngine的說法,因此,這種失敗:

new IntroState(engine_instance) 

Constructors are not inherited,所以實際上基礎類GameStateBaseClass有這樣的構造方法不不意味着IntroState相同。你必須明確地寫了這樣一個構造函數:

class IntroState : public IGameStateInterface 
{ 
public: 
    IntroState(GameEngine & engine) : IGameStateInterface(engine) {} 
}; 

然後,IGameStateInterface需要這樣的委託構造爲好。

編譯器試圖找到一個構造函數一個參數,並且它發現只有一個是IntroState編譯器生成的拷貝構造,具有這種簽名:

IntroState(const IntroState&) 

因此,錯誤消息。

+0

嗯,我不知道構造函數沒有繼承! – Short 2011-12-22 16:13:15

相關問題