[英]Accessing variables from other classes (C++)
我一直在尝试从主类中访问一系列整数,然后在方法中显示它们。
然而,由于我自己对一种我没有编写太长时间的语言的无能,我一直遇到一些麻烦。
经过相当多的搜索,我一直无法找到任何可以帮助我的东西。 如果可能的话,我该怎么做呢?
#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_ */
编辑:感谢帮助到目前为止,我已经能够摆脱大多数错误。 剩下的唯一一个是“.. \\ src \\ zoria.cpp:1616:36:错误:'rock'未在此范围内声明”
您不需要构造函数中的类型来访问成员变量,否则编译器会认为您正在尝试声明新的局部变量。
以下是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;
时,会发生对象的实际存在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;
并访问每个成员变量(如maxhealth
, maxmana
, health
mana
, mana
, level
, agility
, strength
, healthpotions
, manapotions
, armourlevel
, weaponlevel
, crystals
, gold
, rock
和wood
),如:
inv.gold = 10;
inv.rock++;
等整个代码。
并显示库存替换所有冗余显示代码:
inv.printinv();
到处。
在这里查看zoria.cpp ,我已经对所有显示代码进行了更改并更改了变量访问: maxhealth
, maxmana
, health
和healthpotions
您还必须为其余变量执行此操作,例如: mana
, level
, agility
, strength
, manapotions
, armourlevel
, weaponlevel
, crystals
, gold
, rock
和wood
。
希望这可以帮助。 告诉我们是否还有其他问题。
在Inventory.h中,您定义成员变量(maxhealth,maxmana等),在Inventory.cpp中,您应该在其构造函数中声明它们。 问题是你要重新声明它们。 尝试从构造函数中的每个变量中删除“int”,因为您只需在定义变量时指定类型。
这里有几点:
在构造函数中,您通过方法本地重新定义具有相同名称的变量来遮蔽实例变量。 请查看如何在构造函数中初始化实例成员变量。
您尝试将inventory
类的实例隐式转换为带有getinv()
方法签名的std::string
,但您尚未为此类转换定义任何运算符。
(可能与(2)相关)从你发布的错误中,我假设你正在使用getinv()
inventory my_inventory{}; int inv = my_inventory.getinv();
这不起作用,因为没有从std::string
到int
定义的隐式转换。 将inventory::getinv()
的返回类型更改为int
并返回实际的int
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.