我有以下层次结构:
我有以下层次结构:
GameStateBaseClass -> IGameStateInterface -> IntroState
我遇到的问题是,当我使用 GameEngine 引用(如我在 GameStateBaseClass 中定义的那样)实例化 IntroState 时,出现以下错误:
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 引用?
相应的代码如下:
游戏状态基础类.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...
}
介绍状态.hpp
class IntroState : public IGameStateInterface
{
virtual void Init() {}
virtual void Cleanup() { }
// other empty bodies
}
这是游戏引擎 .hpp 文件, 游戏引擎.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 引用(如我在 GameStateBaseClass 中定义的那样)实例化 IntroState 时,出现以下错误:
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 引用?
相应的代码如下:
游戏状态基础类.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...
}
介绍状态.hpp
class IntroState : public IGameStateInterface
{
virtual void Init() {}
virtual void Cleanup() { }
// other empty bodies
}
这是游戏引擎 .hpp 文件, 游戏引擎.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,而不是它在错误消息中也试图转换的构造函数。
有什么想法吗?
最佳答案
您的类 IntroState
没有可以接受类型为 GameEngine
的参数的构造函数,因此,这失败了:
new IntroState(engine_instance)
Constructors are not inherited ,因此基类 GameStateBaseClass
具有这样的构造函数这一事实并不意味着 IntroState
具有相同的构造函数。你必须显式地编写这样一个构造函数:
class IntroState : public IGameStateInterface
{
public:
IntroState(GameEngine & engine) : IGameStateInterface(engine) {}
};
那么,IGameStateInterface
也需要这样一个委托(delegate)构造函数。
编译器尝试找到一个接受一个参数的构造函数,它找到的唯一一个是 IntroState
的编译器生成的复制构造函数,它具有以下签名:
IntroState(const IntroState&)
因此出现错误消息。
关于c++构造函数使用不正确的参数类型来构造对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8601365/