我有一个Weapon.h / cpp类。多数民众赞成有一个枚举
Weapon.h
enum WEAPONTYPE {
LASER,
ROCKET
}
我有一个变量,我想用它来跟踪当前的枚举。
WEAPONTYPE currentWeapon;
我有一个函数将被调用来改变currentWeapon值。头文件中的声明是:
void weaponSelect(WEAPONTYPE choice);
并且.cpp文件中的声明是
void Weapon::weaponSelect(Weapon::WEAPONTYPE enumChoice)
{
currentWeapon = enumChoice;
}
现在我收到的错误是:
错误C2511:'void Weapon :: weaponSelect(Weapon :: WEAPONTYPE)':在'武器'中找不到重载的成员函数
感谢任何帮助。
编辑1:
Weapon.h
#ifndef WEAPON_H_
#define WEAPON_H_
class Weapon
{
public:
Weapon(Ogre::SceneManager* localManager);
virtual ~Weapon(void);
void createBullet(Ogre::Vector3 cameraPosition); //Create bullet nodes/entities.
void weaponSelect(WEAPONTYPE enumChoice); //Function to select weapon type. Tried to have "WeaponType enumChoice" as parameter but would produce error.
void updateBullet(); //Update bullet logic.
enum WEAPONTYPE {
LASER = 0,
ROCKET = 1
};
private:
WEAPONTYPE currentWeapon; //Enum var to track weapon selected.
Ogre::SceneManager* localBulletSceneManager; //Pointer to our application's scene manager
std::vector<Ogre::SceneNode*> bullets; //List of pointers to the bullet nodes.
};
#endif
Weapon.cpp
#include "Weapon.h"
#include <OgreStringConverter.h>
using Ogre::SceneNode;
using Ogre::Entity;
using Ogre::String;
using Ogre::Vector3;
Weapon::Weapon(Ogre::SceneManager* localManager)
: localBulletSceneManager(nullptr)
, currentWeapon(LASER)
{
this->localBulletSceneManager = localManager;
}
Weapon::~Weapon(void)
{
}
void Weapon::weaponSelect(WEAPONTYPE enumChoice)
{
this->currentWeapon = enumChoice;
}
void Weapon::createBullet(Vector3 cameraPosition)
{
//Pointers to use for Quick Node and Entity Creation - Get Reused once object is attached to scene.
SceneNode* tempNode = nullptr;
Entity* tempEntity = nullptr;
//All our Objects are spheres, so create one mesh and reuse it with each entity.
String bulletMesh = "Bullet";
//Procedural::SphereGenerator().setRadius(1.f).setUTile(5.).setVTile(5.).realizeMesh(bulletMeshName);
Procedural::ConeGenerator().setRadius(0.5F).setHeight(3.0F).realizeMesh(bulletMesh);
for (int bulletAmount = 0; bulletAmount < 10; ++bulletAmount)
{
tempNode = this->localBulletSceneManager->getRootSceneNode()->createChildSceneNode("RocketNode" + Ogre::StringConverter::toString(bulletAmount));
tempEntity = this->localBulletSceneManager->createEntity("RocketEntity" + Ogre::StringConverter::toString(bulletAmount), bulletMesh);
//tempEntity->setMaterial(
tempNode->attachObject(tempEntity);
tempNode->setPosition(0,0,100 + (bulletAmount * 10));
}
switch (this->currentWeapon)
{
case LASER:
break;
case ROCKET:
break;
}
}
编辑2: 虚空武器的恢复声明选择原始版本的.h和.cpp,其他海报不做任何修改。
答案 0 :(得分:3)
将您的WEAPONTYPE
声明移到课程顶部:
class Weapon {
public:
enum WEAPONTYPE {
LASER = 0,
ROCKET = 1
};
Weapon(Ogre::SceneManager* localManager);
// ...
错误是由编译器读取行时引起的:
void weaponSelect(WEAPONTYPE enumChoice);
它无法弄清WEAPONTYPE
是什么。之所以发生这种情况,是因为enum
的声明在课堂后期出现。
另外,我发现你正在使用C ++ 11:改为使用enum class
:
enum class weapon {
laser,
rocket
};
答案 1 :(得分:2)
如果在类外声明WEAPONTYPE,请使用以下语法:
void Weapon::weaponSelect(WEAPONTYPE enumChoice)
{
currentWeapon = enumChoice;
}
如果在类中声明了这个枚举:
void Weapon::weaponSelect(Weapon::WEAPONTYPE enumChoice)
{
this->currentWeapon = enumChoice;
}
您必须在使用之前声明枚举。
答案 2 :(得分:0)
尝试将枚举WEAPONTYPE
移动到weaponSelect
的减速之前
另外,我不明白你的API:
一方面,您将weaponSelect
声明为公开。但是,它的参数是受保护的枚举。您班级的用户如何使用它?这应该都具有相同的可访问性/.