我很难解释这个问题,因此这是我的代码的简单总结:
想象一下,我有一个名为“角色”的课程
#include "myEnums"
#include "weapon"
character {
protected:
string characterName;
weapon* myWeapon;
public:
string getCharacterName();
void setCharacterName( string );
string getMyWeapon();
void setMyWeapon();
}
然后在“ setMyWeapon”中,使用此简化代码。
void character::setMyWeapon() {
this->myWeapon = new weapon("longsword");
//this->myWeapon = new weapon(myEnums::LONGSWORD); //Ideally this
}
string getMyWeapon() {
return this->myWeapon.tostring();
}
但是当我输入“。”时“ myWeapon”没有成员,有人知道吗?假设在“ weapon.h”中定义了“ tostring”。
最佳答案
由于myWeapon
是指针,因此您需要取消引用它以访问指针对象:
myWeapon->tostring()
// ^^^^