2015-07-11 39 views
-1

我有以下文件中的一個類文件訪問的變量.hpp:從另一個

#include <iostream> 
#include <cstdlib> 

using namespace std; 

class shop{ 
    public: 
    string shopOption; 
     string shopOptions[6]= {"Buy", "buy", "Sell", "sell", "Leave", "leave"}; 
     string shopInv[3]= {"Sword", "Potion", "Arrows x 25"}; 
     int shopInvAmount= sizeof(shopInv)/sizeof(shopInv[0]); 
     int shopPrices[3]= {250, 55, 70}; 

    shop(){ 
     cout << "Shopkeeper: We buy, we sell, what's your buisness?" << endl; 
    } 
    void store(void){ 
     getline(cin,shopOption); 

     if(shopOption.compare(shopOptions[0]) == 0 || shopOption.compare(shopOptions[1]) == 0){ 
      buy(); 
     } 

     else if(shopOption.compare(shopOptions[2]) == 0 || shopOption.compare(shopOptions[3]) == 0){ 
      sell(); 
     } 

     else if(shopOption.compare(shopOptions[4]) == 0 || shopOption.compare(shopOptions[5]) == 0){ 
      leave(); 
     } 
    } 

    void buy(){ 
     srand(time(0)); 
     string buyQuotes[3]= {"What are you buyin', hon?", "Make it quick, I ain't got all day.", "Another day, another sell."}; 
     int quotePick= rand() % sizeof(buyQuotes)/sizeof(buyQuotes[0]) - 1; 
     if (quotePick < 0){ 
      quotePick= 0; 
     } 

     else if (quotePick > (sizeof(buyQuotes)/sizeof(buyQuotes))){ 
      quotePick= sizeof(buyQuotes)/sizeof(buyQuotes); 
     } 
     cout << "TEST:" << sizeof(shopInv)/sizeof(shopInv[0]) << endl; 
     cout << buyQuotes[quotePick] << endl; 
     cout << "SHOP INVENTORY" << endl << "--------------" << endl; 
     cout << endl; 
     for (int i=0; i < sizeof(shopInv)/sizeof(shopInv[0]); i++){ 
      cout << shopInv[i]<< ": " << shopPrices[i] << endl; 
     } 
     cout << endl << "What'll it be?:"; 
     getline(cin,shopOption); 
    } 
    void sell(){ 

    } 

    void leave(){ 

    } 
}; 

a第二player.hpp

class player{ 
    public: 
    int playerHP= 18; 
    string playerInv[5] {}; 
    int playerGold= 355; 
}; 

現在,我想做些什麼,是之後的字符選擇他們想要購買的物品,而它德量,(不編程還)檢查的價格組合物品,並查看角色手頭上是否有足夠的錢,如果角色購買物品,則將其添加到玩家的庫存中。

但是我想保留商店使用的值,以及與不同類文件中播放器相關的所有內容。 事情是,我不知道如何拉這樣的事情。

那麼,是否有可能從另一個文件中的另一個類訪問一個類的變量? 如果不是,你會如何解決這個問題?

回答

0

從這裏開始閱讀:How does the compilation/linking process work?以獲得多個文件爲您工作。無論您使用哪種編碼環境,都會爲您自動執行該過程,因此您的賠率相當不錯。

然後考慮把項目類

class Item 
{ 
public: 
    Item(string name, int price): mName(name), mPrice(price) 
    { 
    } 
    string getName() 
    { 
     return mName; 
    } 
    string getPrice() 
    { 
     return mPrice; 
    } 
    // other functions 
private: 
    string mName; 
    int mPrice; 
    // other stuff 
} 

在店鋪和播放器,KEEP項

vector<Item> items; 

當玩家試圖購買商品,發現它在列表中的列表,確保玩家可以負擔得起,將其從商店列表中移除並將其添加到玩家列表中。

+0

說實話,我還是非常瞭解C++,我還沒有學到,所以我得詳細閱讀。 一個問題:在一個構造函數之後把一些東西放在後面,到底是什麼?這是我第一次看到這樣的事情。 至於開發環境,我真的只使用Gedit/Vim/Notepad ++和MinGW/Make,所以我懷疑什麼是自動化的,爲什麼這是一個重要的因素呢? – Arizodo

+0

@Arizodo''''告訴編譯器有一個要初始化的成員變量列表,所以'mName(name)'在構造函數開始在花括號內運行代碼之前將參數名賦給類成員mName。當你有一個繼承自基類的子類並且基類需要在子類之前初始化時,這一點非常重要。開發環境通過執行一些任務使您的工作更輕鬆,比如安排多文件項目正確構建。 Make最終更強大/多功能,但學習曲線陡峭。 – user4581301

+0

謝謝,我得儘快解決這個問題。 – Arizodo