从其他类访问变量(C ++)

时间:2016-06-23 04:31:53

标签: c++ class variables integer

我一直在尝试从主类中访问一系列整数,然后在方法中显示它们。

然而,由于我自己对一种语言的无能,我已经有点麻烦了,因为我没有编写太久的语言。

经过相当多的搜索,我找不到任何可以帮助我的东西。如果可能的话,我该怎么做呢?

#include "inventory.h"

inventory::inventory(){
    int maxhealth = 100;
    int maxmana = 0;

    int health = 100;
    int mana = 0;
    int level = 1;

    int agility = 1;
    int strength = 1;

    int healthpotions = 0;
    int manapotions = 0;

    int armourlevel = 0;
    int weaponlevel = 0;

    int crystals = 0;

    int gold = 0;
    int rock = 0;
    int wood = 0;
}

string inventory::getinv(){
    return inventory; //I know this sort of return thing won't work, just a placeholder until I figure out what to do.
}

这是我迄今为止所使用的内容,但我很难做到这一点,甚至没有显示会员' X'没有在这个构造函数中初始化。"我显然做错了。

Inventory.h:

#ifndef INVENTORY_H_
#define INVENTORY_H_

#include <iostream>

class inventory{
private:
    int maxhealth;
    int maxmana;

    int health;
    int mana;
    int level;

    int agility;
    int strength;

    int healthpotions;
    int manapotions;

    int armourlevel;
    int weaponlevel;

    int crystals;

    int gold;
    int rock;
    int wood;

public:
    inventory();
    string getinv();
};
#endif /* INVENTORY_H_ */
编辑:感谢迄今为止的帮助,我已经能够摆脱大部分错误。剩下的唯一一个是&#34; .. \ src \ zoria.cpp:1616:36:错误:&#39;摇滚&#39;未在此范围内宣布&#34;

3 个答案:

答案 0 :(得分:0)

您不需要构造函数中的类型来访问成员变量,否则编译器会认为您正在尝试声明新的局部变量。

以下是inventory.cpp文件的基本更正:

#include "inventory.h"

inventory::inventory(){ // this is the constructor
    maxhealth = 100;
    maxmana = 0;

    health = 100;
    mana = 0;
    level = 1;

    agility = 1;
    strength = 1;

    healthpotions = 0;
    manapotions = 0;

    armourlevel = 0;
    weaponlevel = 0;

    crystals = 0;

    gold = 0;
    rock = 0;
    wood = 0;
}

string inventory::getinv(){
    return "inventory"; //I know this sort of return thing won't work, just a placeholder until I figure out what to do.
}

请注意,""已添加到占位符(return "inventory"),使其成为有效的string

注意:如果没有类的对象,您无法直接访问类中的变量,即使它被声明为public(static是唯一的例外)。但是,您已将所有成员变量声明为private,因此需要getter和setter函数来访问它们的值。

修改

类就像包含东西的容器。但只是类的定义就像模板一样,可用于创建该类型的对象。当您编写inventory someobject;时会发生对象的实际存在。

现在每个类都有一个名为构造函数的特殊函数,该函数按类本身的名称进行操作,并在声明此类的对象后立即调用。您可以在构造函数中初始化类的所有成员变量。

要访问类的成员,您必须使用.点运算符。如果成员需要在班级主体外直接访问,则必须声明成员public

所以你改变了这样的类定义:

<强> inventory.h:

#ifndef INVENTORY_H_
#define INVENTORY_H_
class inventory{
public:

    int maxhealth;
    int maxmana;

    int health;
    int mana;
    int level;

    int agility;
    int strength;

    int healthpotions;
    int manapotions;

    int armourlevel;
    int weaponlevel;

    int crystals;

    int gold;
    int rock;
    int wood;

    inventory();
    void printinv();
};
#endif /* INVENTORY_H_ */

<强> inventory.cpp:

#include "inventory.h"
#include <iostream>
using namespace std;

inventory::inventory()
{
    maxhealth = 100;
    maxmana = 0;

    health = 100;
    mana = 0;
    level = 1;

    agility = 1;
    strength = 1;

    healthpotions = 0;
    manapotions = 0;

    armourlevel = 0;
    weaponlevel = 0;

    crystals = 0;

    gold = 0;
    rock = 0;
    wood = 0;
}

void inventory::printinv(){
    cout << "LEVEL:           " << level << endl;
    cout << "HEALTH:          " << health << endl;
    cout << "MANA:            " << mana << endl;
    cout << "AGILITY:         " << agility << endl;
    cout << "STRENGTH:        " << strength << endl;
    cout << endl;
    cout << "HEALTH POTIONS:  " << healthpotions << endl;
    cout << "MANA POTIONS:    " << manapotions << endl;
    cout << endl;
    cout << "ARMOUR LEVEL:    " << armourlevel << endl;
    cout << "WEAPON LEVEL:    " << weaponlevel << endl;
    cout << "CRYSTALS:        " << crystals << endl;
    cout << endl;
    cout << "GOLD:            " << gold << endl;
    cout << "ROCK:            " << rock << endl;
    cout << "WOOD:            " << wood << endl;
}

现在在main中声明该类的对象,如:

inventory inv;

并访问每个成员变量(例如maxhealthmaxmanahealthmanalevelagility,{{1 },strengthhealthpotionsmanapotionsarmourlevelweaponlevelcrystalsgoldrock )喜欢:

wood

等。整个代码。

并显示库存,将所有冗余显示代码替换为:

inv.gold = 10;
inv.rock++;

随处可见。

请参阅zoria.cpp here,我已对所有显示代码进行了更改,并更改了inv.printinv(); maxhealthmaxmanahealth您的变量访问权限还必须对其余变量执行此操作,例如:healthpotionsmanalevelagilitystrengthmanapotions,{ {1}},armourlevelweaponlevelcrystalsgold

希望这会有所帮助。告诉我们是否还有其他问题。

答案 1 :(得分:0)

在Inventory.h中,您定义成员变量(maxhealth,maxmana等),在Inventory.cpp中,您应该在其构造函数中声明它们。问题是你要重新声明它们。尝试删除&#34; int&#34;来自构造函数中的每个变量,因为您只需在定义变量时指定类型。

答案 2 :(得分:0)

这里有几点:

  1. 在构造函数中,您通过方法本地重新定义同名变量来实现实例变量。请查看如何在构造函数中初始化实例成员变量。

  2. 您尝试使用inventory方法的签名隐式将std::string类的实例转换为getinv(),但您尚未定义任何此类运算符转换。

  3. (可能与(2)相关)从您发布的错误中,我假设您正在使用{/ 1}}

    的内容
    getinv()

    这不起作用,因为没有从inventory my_inventory{}; int inv = my_inventory.getinv(); std::string定义的隐式转换。将int的返回类型更改为inventory::getinv()并在那里返回实际的int