与c++模板有点混淆
Little confused with C++ templates
本文关键字:c++ 更新时间:2023-10-16
我有以下内容:
extern Keyboard keyboard;
enum DEVICETYPE
{
KEYBOARD,
MOUSE,
CONTROLLER,
TOUCH
};
template <typename T>
class PlayerInputDevice
{
public:
void SetDevice( DEVICETYPE deviceType )
{
switch( deviceType )
{
case KEYBOARD:
Device = &keyboard;
break;
}
}
T Device;
};
我得到编译错误:
Error 1 error C2955: 'Game::Model::Input::PlayerInputDevice' : use of class template requires template argument list (ConcretePlayer.cpp) c:usersjamesdocumentsvisual studio 2013projectsgamesjimmymodelconcreteplayer.h 29 1 Model
Error 2 error C2512: 'Game::Model::Input::PlayerInputDevice' : no appropriate default constructor available c:usersjamesdocumentsvisual studio 2013projectsgamesjimmymodelconcreteplayer.cpp 8 1 Model
Error 6 error C2955: 'Game::Model::Input::PlayerInputDevice' : use of class template requires template argument list c:usersjamesdocumentsvisual studio 2013projectsgamesjimmymodelconcreteplayer.h 29 1 Game
7 IntelliSense: argument list for class template "Game::Model::Input::PlayerInputDevice" is missing c:UsersJamesDocumentsVisual Studio 2013ProjectsGamesJimmyModelConcretePlayer.h 29 5 Model
不确定我是否理解正确?
我希望能够这样说:
class Player
{
public:
PlayerInputDevice PlayerInputDevice;
}
...
player.PlayerInputDevice.SetDevice( KEYBOARD );
player.PlayerInputDevice.IsUpPressed();
你的PlayerInputDevice
是一个模板类,所以要使用它,你需要一个模板参数,正如编译器所说。例如
class Player
{
public:
PlayerInputDevice<Device> playerInputDevice;
};
其中Device
是适合您的应用程序的类型。
注意我已将数据成员的名称更改为playerInputDevice
;不能与类名相同
相关文章:
- 没有找到相关文章