2013-09-23 40 views
0

我在將值輸入到放置在類中的數組中時遇到問題。我嘗試通過使用方法來做到這一點。我的代碼如下所示:C++中的其他類中的類類型數組

#include <iostream> 

using namespace std; 

//class for items 
class Item{ 
string name; 
int amount; 
public: 
Item(); 
Item(string, int); 

//returns the name of an item 
string getName(){ 
    return name; 
} 
//sets name for items 
void setName(string name){ 
    this->name = name; 
} 

//returns the amount of items 
int getAmount(){ 
    return amount; 
} 
//sets the amount for items 
void setAmount(int amount){ 
    this->amount = amount; 
} 
}; 

//first constructor 
Item::Item(){ 
name="none"; 
amount=0; 
} 

//second constructor 
Item::Item(string name, int amount){ 
this->name=name; 
this->amount=amount; 
} 

//class for hero with "Items" array 
class Hero{ 
string name; 
Item inventory[20]; 
public: 
Hero(); 
Hero(string); 

//returns the name of the hero 
string getName(){ 
    return name; 
} 
//sets the name for the hero 
void setName(string name){ 
     this->name = name; 
} 
}; 

//first constructor 
Hero::Hero(){ 
name="none"; 
} 

//second constructor 
Hero::Hero(string name){ 
this->name=name; 
} 

int main() { 
Hero firsthero; 
string name; 
//giving hero the name 
cout<<"Input name: "; 
cin>>name; 
firsthero.setName(name); 
//printing the hero's name 
cout<<firsthero.getName()<<endl; 
//setting the items; 
Item sword; 
Item armor; 
Item potion; 
//setting items' values; 
sword.setName("sword"); 
sword.setAmount(1); 
armor.setName("armor"); 
armor.setAmount(1); 
potion.setName("potion"); 
potion.setAmount(3); 
//gives these items into array "inventory" in the "firsthero" class 
return 0; 
} 

我想補充項目「亮劍」,「鎧甲」和「魔藥」到firsthero,但是我還沒有找到一種方法來寫一個方法中的「英雄」,這將允許我這樣做。我可以直接通過公開它的字段來加載它們,但我讀到它不被推薦。

回答

0

而不是在Hero類中擁有大小爲20的數組,請嘗試使用std::vector。如果您有HeroaddItem方法,您可以簡單地檢查一下英雄是否可以容納物品(檢查inventory.size()針對您創建的某個常量,可能類似於MAX_INVENTORY_SIZE)。

因此,像這樣:

std::vector<Item> inventory; 
const int MAX_INVENTORY_SIZE = 20; 
... 

//returns true if it successfully added the item, false otherwise 
bool Hero::addItem(Item i) { 
    if(inventory.size < MAX_INVENTORY_SIZE) { 
     inventory.push_back(i); 
     return true; 
    } 

    return false; 
} 

... 

hero.addItem(sword); 
hero.addItem(armor); 
hero.addItem(potion); 
+0

如果他有一個恆定的最大尺寸,'的std :: array'是一個更好的選擇比'std :: vector'。如果他想通過邊界檢查搞砸邏輯,矢量會很高興地讓他繼續添加元素(以後可能會導致問題,也可能不會導致問題)。數組會給出一個超出界限的異常(至少如果他使用'at'方法) –

+1

是的,但如果他希望隨機刪除'Item's,那麼他將不得不跟蹤'unfilled '數組中的索引。 –

0

你需要寫的存取方法爲您的項目:

class Hero 
{ 
private: 
    std::string name; 
    std::array<Item, 20> inventory; // use the array container instead of C-style arrays 
    unsigned int itemCount; 

public: 
    Hero() : itemCount(0) {} 
    Hero(string n) : name(n), itemCount(0) {} 

    //returns the name of the hero 
    string getName() 
    { 
     return name; 
    } 
    //sets the name for the hero 
    void setName(const std::string& name) 
    { 
     this->name = name; 
    } 

    const Item& getItem(int i) const { return inventory[i]; } // NOTE: you may want some bounds checking here, but this is basically what you are doing 
    Item& getItem(int i) { return inventory[i]; } // to update a particular item 

    void addItem(const Item& item) { inventory[itemCount++] = i; } // same with bounds checking here 
};