Glen Morse
Glen Morse

Reputation: 2593

Can not get function to work

I want when this function is ran (Monster.cpp)

void Monster::addWeapon(typeOfWeapon selection){
    cout<<myWeaponSelection<<endl; //out puts correctly.
    myWeaponSelection[0].getWeapon(selection); //gives error    
}

that it would run this function (weapon.cpp)

void getWeapon(typeOfWeapon myWeaponSelection)
{
    cout<<"you got weapon!"<<myWeaponSelection<<endl
}

ERROR: monster.cpp(126) : error C2228: left of '.getWeapon' must have class/struct/union

Monster.h

#ifndef MONSTER
#define MONSTER
#include <string>
using namespace std;
enum typeOfMonster {dog,wolf,bear,goat,human,god,snail,elephant,wayven,bird,worm,kid,boy,ent,superman};

class Monster
{
    public:
    Monster();
    ~Monster(){}

    typeOfWeapon myWeaponSelection[2];
    void addWeapon(typeOfWeapon weaponName);
};
#endif

Weapon.h

#ifndef WEAPON
#define WEAPON
#include <string>
using namespace std;
enum typeOfWeapon {bark,howel,eat,ram,shoot,smite,slime,stomp,peck,swarm,dig,bow,slingshot,kick,beem, //wp 1
                    bite,charm,claw,naw,burn,lighingstrike,heal,smack,lazers,poop,posion,superkick,punch,tackel,freeze};//wp 2
class Weapon
{
    public:
    Weapon();
    ~Weapon(){}

    typeOfWeapon myWeaponSelection;
    void getWeapon(typeOfWeapon myWeaponSelection,int whichWeapon); 
};
#endif

How to fix?

Upvotes: 0

Views: 92

Answers (2)

Pierre Fourgeaud
Pierre Fourgeaud

Reputation: 14510

It is because typeOfWeapon is an enum. It does not have member function.

If you want to add a new weapon to myWeaponSelection, you can do :

void Monster::addWeapon(typeOfWeapon selection)
{
    cout << myWeaponSelection << endl;
    myWeaponSelection[0] = selection;
    //                  ^^^^^^^^^^^^^
}

because myWeaponSelection[0] and selection are of the same type.

And your getWeapon should be in the Weapon scope :

void Weapon::getWeapon( typeOfWeapon myWeaponSelection );

not

void getWeapon( typeOfWeapon myWeaponSelection );

Upvotes: 3

juanchopanza
juanchopanza

Reputation: 227370

You have a non member function in your Weapon.cpp:

void getWeapon(typeOfWeapon myWeaponSelection)

You need to put is in the Weapon scope:

void Weapon::getWeapon(typeOfWeapon myWeaponSelection)
//   ^^^^^^^^

Upvotes: 3

Related Questions