无法获得功能

时间:2013-07-11 06:53:23

标签: c++

我希望在运行此功能时(Monster.cpp)

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

它将运行此功能(weapon.cpp)

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

错误: monster.cpp(126):错误C2228:'。getWeapon'的左边必须有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

如何解决?

2 个答案:

答案 0 :(得分:3)

您的Weapon.cpp

中有非会员功能
void getWeapon(typeOfWeapon myWeaponSelection)

您需要将{存放在Weapon范围:

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

答案 1 :(得分:3)

这是因为typeOfWeapon是一个枚举。它没有成员函数。

如果要向myWeaponSelection添加新武器,可以执行以下操作:

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

因为myWeaponSelection[0]selection属于同一类型。

你的getWeapon应该在Weapon范围内:

void Weapon::getWeapon( typeOfWeapon myWeaponSelection );

void getWeapon( typeOfWeapon myWeaponSelection );