2013-10-22 31 views
0

我有硬一點點時間地名釋義的問題,所以這裏是我的代碼的簡單概要:可供對象沒有成員聲明爲類變量

想象一下我所說的「性格」一類

#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」定義...

回答

1

由於myWeapon是一個指針,你需要提領它來訪問指針對象的成員:

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