[英]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.