craigwp2703
craigwp2703

Reputation: 29

Passing an enum as a parameter

I have a Weapon.h/cpp class. thats has an enum

Weapon.h
enum WEAPONTYPE {
    LASER,
    ROCKET
}

and I have a variable that I'd like to use to track the current enum.

WEAPONTYPE currentWeapon;

I have a function that will be called to change the currentWeapon value. The declaration in the header file is:

void weaponSelect(WEAPONTYPE choice);

and the declaration in the .cpp file is

void Weapon::weaponSelect(Weapon::WEAPONTYPE enumChoice)
{
    currentWeapon = enumChoice;
}

Now the error I recieve is:

error C2511: 'void Weapon::weaponSelect(Weapon::WEAPONTYPE)' : overloaded member function not found in 'Weapon'

Any help is appreciated.

Edit 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;
    }
}

Edit 2: Reverted declaration for void weaponSelect in both .h and .cpp to the original version without any changes suggested by other posters.

Upvotes: 0

Views: 5502

Answers (3)

egur
egur

Reputation: 7980

If WEAPONTYPE is declared outside the class use this syntax:

void Weapon::weaponSelect(WEAPONTYPE enumChoice)
{
    currentWeapon = enumChoice;
}

If this enum is declared within the class:

void Weapon::weaponSelect(Weapon::WEAPONTYPE enumChoice)
{
    this->currentWeapon = enumChoice;
}

You must declare the enum before using it.

Upvotes: 2

yosim
yosim

Reputation: 503

try moving the enum, WEAPONTYPE to be before the deceleration of weaponSelect Also, I dont understand your API: On one hand, you declare weaponSelect as public. But, it's parameter is a protected enum. how cau users of your class can use it? this should all have the same accessibility/.

Upvotes: 0

Shoe
Shoe

Reputation: 76300

Move your WEAPONTYPE declaration on the top of the class:

class Weapon {
public:
    enum WEAPONTYPE {
        LASER = 0,
        ROCKET = 1
    };

    Weapon(Ogre::SceneManager* localManager);
    // ...

The error was cause by the fact that when the compiler read the line:

void weaponSelect(WEAPONTYPE enumChoice);

it couldn't figure out what WEAPONTYPE is. That happened because the declaration of the enum comes later in the class.

Also, I see you are using C++11: use enum class instead:

enum class weapon {
    laser,
    rocket
};

Upvotes: 3

Related Questions