c ++构造函数使用不正确的参数类型来构造对象

Sho*_*ort 0 c++ inheritance constructor interface reference

我有以下层次结构:

我有以下层次结构:

GameStateBaseClass -> IGameStateInterface -> IntroState
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是当我使用GameEngine引用实例化一个IntroState时(我在GameStateBaseClass中定义)我得到以下错误:

错误1错误C2664:'IntroState :: IntroState(const IntroState&)':无法将参数1从'GameEngine'转换为'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;
}
Run Code Online (Sandbox Code Playgroud)

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...
}
Run Code Online (Sandbox Code Playgroud)

IntroState.hpp

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

    virtual void Cleanup() { }

    // other empty bodies 

}
Run Code Online (Sandbox Code Playgroud)

这是游戏引擎.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
};
Run Code Online (Sandbox Code Playgroud)

在我的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;
}
Run Code Online (Sandbox Code Playgroud)

我希望它只使用我在GameStateBaseClass中定义的构造函数,该构造函数采用const GameEngine引用来构造IntroState,而不是它在错误消息中尝试转换的那个.

有任何想法吗?

我遇到的问题是当我使用GameEngine引用实例化一个IntroState时(我在GameStateBaseClass中定义)我得到以下错误:

错误1错误C2664:'IntroState :: IntroState(const IntroState&)':无法将参数1从'GameEngine'转换为'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;
}
Run Code Online (Sandbox Code Playgroud)

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...
}
Run Code Online (Sandbox Code Playgroud)

IntroState.hpp

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

    virtual void Cleanup() { }

    // other empty bodies 

}
Run Code Online (Sandbox Code Playgroud)

这是游戏引擎.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
};
Run Code Online (Sandbox Code Playgroud)

在我的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;
}
Run Code Online (Sandbox Code Playgroud)

我希望它只使用我在GameStateBaseClass中定义的构造函数,该构造函数采用const GameEngine引用来构造IntroState,而不是它在错误消息中尝试转换的那个.

有任何想法吗?

Bjö*_*lex 6

您的类IntroState没有可以接受该类型参数的构造函数GameEngine,因此,这会失败:

new IntroState(engine_instance)
Run Code Online (Sandbox Code Playgroud)

构造函数不是继承的,因此基类GameStateBaseClass具有这样的构造函数的事实并不意味着相同IntroState.你必须明确地写这样的构造函数:

class IntroState : public IGameStateInterface
{
public:
    IntroState(GameEngine & engine) : IGameStateInterface(engine) {}
};
Run Code Online (Sandbox Code Playgroud)

然后,还IGameStateInterface需要这样的委托构造函数.

编译器尝试查找带有一个参数的构造函数,并且它找到的唯一一个是编译器生成的复制构造函数IntroState,它具有以下签名:

IntroState(const IntroState&)
Run Code Online (Sandbox Code Playgroud)

因此错误消息.