【问题标题】:No members available for object declared as a class variable没有可用于声明为类变量的对象的成员
【发布时间】:2013-10-31 21:40:57
【问题描述】:

我很难解释这个问题,所以这里是我的代码的简单概要:

假设我有一个名为“角色”的类

#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' 没有成员,有人知道吗?假设 'tostring' 在 'weapon.h' 中定义...

【问题讨论】:

    标签: c++ oop object member


    【解决方案1】:

    由于myWeapon 是一个指针,您需要取消引用它以访问指针对象的成员:

    myWeapon->tostring()
    //     ^^^^
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 2011-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-27
      相关资源
      最近更新 更多