C++ 从一个类文件访问另一个类文件中的变量

C++ 从一个类文件访问另一个类文件中的变量,c++,class,variables,C++,Class,Variables,我有以下文件: main.cpp shop.hpp player.hpp 其中每个文件中都包含以下代码: main.ccp: #include <iostream> #include <cstdlib> #include "shop.hpp" using namespace std; string *inventory= new string[3]; int invGold= 355; int main(void){

我有以下文件:

main.cpp
shop.hpp
player.hpp
其中每个文件中都包含以下代码:

main.ccp:

 #include <iostream>
    #include <cstdlib>
    #include "shop.hpp"

    using namespace std;
    string *inventory= new string[3];
    int invGold= 355;

    int main(void){
    shop store;
    store.store();
    }
现在,我想做的是,在角色选择他们想要购买的物品后,检查组合物品的价格,看看角色手头是否有足够的钱,如果角色购买了物品,将它们添加到玩家的库存中

但是我想将商店使用的值以及与播放器相关的所有内容保存在不同的类文件中。 问题是,我不知道怎么拉这样的东西

那么,不可能从另一个文件althogether中的另一个类访问一个类的变量吗? 如果不是,您建议我如何解决这个问题?

开始阅读这里:让多个文件为您工作。无论您使用的是什么编码环境,都有可能为您实现过程的自动化

然后考虑制作一个项目类

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
}
在商店和播放器中,保留物品列表

vector<Item> items;
向量项;

当玩家试图购买一个物品,在列表中找到它,确保玩家能够负担得起,从商店的列表中删除它并添加到玩家的列表中。

老实说,我仍然很了解C++,我还没有了解到,所以我得读一读。一个问题:在构造器后面的“:”后面放东西到底是做什么的?这是我第一次看到这样的东西。至于开发环境,我实际上只使用Gedit/Vim/Notepad++和MinGW/Make,所以我怀疑任何东西都是自动化的,这到底是一个重要因素吗?@Arizodo
告诉编译器有一个要初始化的成员变量列表,所以
mName(name)
在构造函数开始在大括号内运行代码之前,将参数名分配给类成员mName。当您有一个子类继承自基类并且基类需要在子类之前初始化时,这一点非常重要。开发环境只是通过执行一些任务来简化您的工作,比如安排正确构建多文件项目。Make最终更强大/更通用,但学习曲线更陡峭。谢谢,我很快就要解决这个问题了。
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
}
vector<Item> items;