0

我想要什么时候运行这个函数(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' 左侧必须有类/结构/联合

怪物.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

武器.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

怎么修?

4

2 回答 2

3

你有一个非成员函数Weapon.cpp

void getWeapon(typeOfWeapon myWeaponSelection)

你需要把is放在Weapon范围内:

void Weapon::getWeapon(typeOfWeapon myWeaponSelection)
//   ^^^^^^^^
于 2013-07-11T06:55:01.360 回答
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 );
于 2013-07-11T06:59:38.220 回答