我希望在运行此功能时(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
如何解决?
答案 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 );