2011-12-22 115 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

相关问题