2015-07-11 40 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