简体   繁体   中英

Accessing variables from other classes (C++)

I have been trying to access a series of integers from the main class, and then display them inside a method.

However, I've been having a bit of trouble with this due to my own ineptitude with a language I've not been coding in for too long.

After quite a bit of searching, I have been unable to find anything that can help me. How would I go about doing this, if it's possible at all?

#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.
}

This is what I have been using thus far, but I'm having a hard time even getting that to not display the "Member 'X' was not initialized in this constructor." I'm clearly doing something quite wrong.

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_ */

EDIT: Thanks to the help so far I've been able to get rid of most of the errors. The only one left is "..\\src\\zoria.cpp:1616:36: error: 'rock' was not declared in this scope"

You do not need the type in the constructor to access the member variables, otherwise the compiler would think that you are trying to declare new local variables.

Here is a basic correction for the inventory.cpp file:

#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.
}

Note the "" added to the placeholder ( return "inventory" ) to make it a valid string .

Note: You cannot access the variable that is in a class directly without an object of the class, even if it is declared public ( static being the only exception). However you have declared all your member variables as private and will thus require getter and setter functions to access their values.

EDIT:

Classes are like containers which contain things. But just the definition of a class is just like a stencil which can be used to create objects of that type. The actual existence of the object occurs when you write inventory someobject; .

Now every class has a special function called the constructor which goes by the name of the class itself and is called as soon as the an object of this class is declared. You can initialize all member variables of the class in the constructor.

To access the members of a class you have to use the . dot operator. Members have to be declared public if they need to be directly accessed outside the class' body.

So you change the class definitions like this:

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_ */

and

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;
}

Now declare the object of the class in main like:

inventory inv;

and access every member variable (such as maxhealth , maxmana , health , mana , level , agility , strength , healthpotions , manapotions , armourlevel , weaponlevel , crystals , gold , rock and wood ) like:

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

etc. throughout the code.

and to display the inventory replace all the redundant display code with:

inv.printinv();

everywhere.

See zoria.cpp here , I have done the changes for all display code and changed the variable accesses for: maxhealth , maxmana , health and healthpotions you also have to do it for the rest of the variables like: mana , level , agility , strength , manapotions , armourlevel , weaponlevel , crystals , gold , rock and wood .

Hope this helps. Tell if there are any more questions.

In Inventory.h you define the member variables (maxhealth, maxmana etc) and in Inventory.cpp, you should declare them in its constructor. The problem is that you're redeclaring them. Try removing the "int" from every variable in the constructor because you only need to specify the type when you define the variable.

A few points here:

  1. In you constructor you are shadowing the instance variables by method-local re-definition of identically named variables. Please, look up on how to initialize instance member variables in constructors.

  2. You try to implicitly convert an instance of your inventory class into a std::string with the signature of your getinv() method, but you have not defined any operator for this kind of conversion.

  3. (probably related to (2)) From the error you posted, I assume you are using getinv() something along the line of

     inventory my_inventory{}; int inv = my_inventory.getinv(); 

    This will not work as there is no implicit conversion from std::string to int defined. Change the return type of inventory::getinv() to int and return an actual int there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM