1

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"

Eris
  • 13
  • 7
  • 3
    Please post some code illustrating the problem. – Ahmed Akhtar Jun 23 '16 at 04:35
  • `..\src\inventory.cpp: At global scope: ..\src\inventory.cpp:34:23: error: no 'int inventory::getinv()' member function declared in class 'inventory' int inventory::getinv(){` is the usual form of error I get from it. Is that what you're looking for? – Eris Jun 23 '16 at 04:37
  • Do you need to modify their values or only display them? – Jean Catanho Jun 23 '16 at 04:39
  • Post code form the file `inventory.cpp`. – Ahmed Akhtar Jun 23 '16 at 04:40
  • @JeanCatanho Modify their values would be preferred. – Eris Jun 23 '16 at 04:41
  • @AhmedAkhtar Done! – Eris Jun 23 '16 at 04:42
  • 1
    Not enough post the entire definition of `inventory` class. – Ahmed Akhtar Jun 23 '16 at 04:42
  • @AhmedAkhtar Would you also want the entire main class? – Eris Jun 23 '16 at 04:45
  • Yes the current code you have posted does not contain the error you are mentioning. How many files do you have? – Ahmed Akhtar Jun 23 '16 at 04:46
  • 1
    Your problem is in the `inventory.h` class that I can say by looking at the error you posted. Please post that so I may point out the error. – Ahmed Akhtar Jun 23 '16 at 04:48
  • I've mainly been basing what I've tried to do so far off a Cave of Programming tutorial dealing with strings, so that may explain some errors, sorta. – Eris Jun 23 '16 at 04:52
  • And how do you call these functions? Post `main` too. – Ahmed Akhtar Jun 23 '16 at 04:54
  • I have yet actually tried to call them, as the errors have kind of stopped me from doing anything with it, so I've spent most of my time trying to figure out how to stop said errors. I'm hoping a void method will work, though. One of the other errors I've been getting is "In file included from ..\src\inventory.cpp:8:0: ..\src\inventory.h:15:2: error: 'maxhealth' does not name a type" – Eris Jun 23 '16 at 04:57
  • See my answer below. – Ahmed Akhtar Jun 23 '16 at 04:59
  • And what is in this file `zoria.cpp`? – Ahmed Akhtar Jun 23 '16 at 05:06
  • Zoria.cpp is the main file, and it houses most of the code. Due to me being unable to store things such as the inventory in other classes, it's much larger than it should be. – Eris Jun 23 '16 at 05:07
  • Post it then so that we may see where this last error arises. At least post line `1616` and some others around it. – Ahmed Akhtar Jun 23 '16 at 05:08
  • @AhmedAkhtar Line 1616 is just a place where I have tried to access the variable from by printing it to console. cout << "ROCK: " << rock << endl; – Eris Jun 23 '16 at 05:16
  • No you cannot access the variable that is in a class directly without an object of the class, even if it is declared `public`. However you have declared all your member variables as `private` and will thus require getter and setter functions to access their values. – Ahmed Akhtar Jun 23 '16 at 05:18
  • I see. I was basing it off a getter-setter function, which might not work well in this scenario. – Eris Jun 23 '16 at 05:21
  • Then you may want to declare them `public` or alternatively, you may use a `friend` function. – Ahmed Akhtar Jun 23 '16 at 05:23
  • I'll declare them public then. Preferably, I'd like to have a method that prints a block of text with each integer assigned to it, so it'd print "GOLD: (goldnumber), along with most of the other integers. – Eris Jun 23 '16 at 05:24
  • So you can define `public` methods inside the `inventory` class to do that and then call them wherever you want to print them. BTW are you creating any object of `inventory` class in your code? – Ahmed Akhtar Jun 23 '16 at 05:27
  • I have an object titled inventory inv; inside the main class, and I am hoping to use the getinv method to display the text by using the return feature. – Eris Jun 23 '16 at 05:31
  • You can try declaring some friend functions so that you can access the private and protected members of the methods in other classes also – Coding Ninja Jun 23 '16 at 06:47

3 Answers3

0

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.

Ahmed Akhtar
  • 1,444
  • 1
  • 16
  • 28
  • 3
    `using namespace std;` is bad practice, [don't encourage it](http://stackoverflow.com/q/1452721/3980929) – Rakete1111 Jun 23 '16 at 05:12
  • @Rakete1111 Thanks I did not know this. – Ahmed Akhtar Jun 23 '16 at 05:14
  • What would be the alternative to namespace std then? Would I need to create my own namespace? – Eris Jun 23 '16 at 05:18
  • @DeiniolL I have removed that suggestion from my answer because it was trivial and did nothing to solve the actual error. And it was discouraged too, so don't worry about that. – Ahmed Akhtar Jun 23 '16 at 05:21
  • @DeiniolL Then add the code with `cout<<"GOLD: ("< – Ahmed Akhtar Jun 23 '16 at 05:36
  • I am not sure what you mean by that, but the main issues now are just the "integer was not declared in this scope" from the main class, and me being unable to use cout in the method without namespace std. I've found that this sort of method is the only way that works, and displays it in a way that isn't all on one line. `cout << "integer name: " << integer << endl;` However, this outputs nothing when called with an object while using the call thing of inv.getinv(); – Eris Jun 23 '16 at 05:47
  • @DeiniolL Using `cout` like `std::cout` is the recommended way of doing it, but if you still want to avoid doing that, then add `using namespace std;` after `#include ` as I suggested earlier. – Ahmed Akhtar Jun 23 '16 at 05:50
  • @DeiniolL Can you post some lines from your `main` too, it is very ambiguous what you are trying to do. You do not have any variable named `integer` that is why this error is coming. – Ahmed Akhtar Jun 23 '16 at 05:55
  • The integer was just changed as a placeholder, as it's many integers are doing it. You can probably replace it with health, manapotions, or most of the other integers I have moved over. With the thing you said above that, I'll see if I can use the std::cout one. – Eris Jun 23 '16 at 05:57
  • @DeiniolL Is this line `cout << "integer name: " << integer << endl;` you mentioned, in `main` function or `getinv` function? – Ahmed Akhtar Jun 23 '16 at 06:03
  • It's in the getinv function. There's a few things of it left in the main due to me not migrating it over yet, but if this works, it will only be in the getinv fucntion. – Eris Jun 23 '16 at 06:06
  • Thank you so much for your help so far. Even though the issue hasn't been resolved yet, I'm light years ahead of where I would be otherwise. ^^ – Eris Jun 23 '16 at 06:09
  • @DeiniolL You are welcome, please mark the answer as accepted, if it has helped you, by clicking on the tick mark next to it. One last thing, you have to move all of the `cout << "integer name: " << integer << endl;` to the `getinv` function to make it work because they wont work in the `main` function without appropriate usage with an object of the class (also declaring them public or using getters and setters). – Ahmed Akhtar Jun 23 '16 at 06:13
  • It's yet to be solved though, as I am still getting things like 'wood was not declared in this scope' from the main class. Also, all of those couts are already in the function. The call method of "inv.getinv();" does also not work for the same error of "not declared in this scope". Would I also need to add `#include "inventory.cpp"` alongside `"include "inventory.h"`? – Eris Jun 23 '16 at 06:21
  • @DeiniolL Remove `#include "inventory.h` and add `#include "inventory.cpp` only. As I said, it will continue to give `* was not declared in this scope` errors until you move all of the lines to `getinv` function. One thing I don't understand though is that, what do you actually want `getinv` function to do? – Ahmed Akhtar Jun 23 '16 at 06:28
  • Because of how the game is coded in, every time I want to display the inventory, I have to make a separate statement declaring it, which results in me having to copy the same thing over and over again, or if I want to change it, I need to edit every instance of it. Also, one I move over those lines, I'd still need to change examples such as rock++ when grabbing an item off the ground, or am I wrong? – Eris Jun 23 '16 at 06:34
  • Okay, even when changing it to .cpp, and the error going away, calling it with inv.getinv(); does not output text. – Eris Jun 23 '16 at 06:36
  • OK I will have to see your `main` file. Please paste all its code [here](http://hastebin.com/) save using `ctrl+s` and paste the generated link from the browsers link bar in a comment so that I can view the code. Read more about hastebin [here](http://hastebin.com/about.md). – Ahmed Akhtar Jun 23 '16 at 06:41
  • @DeiniolL I think you have gravely misunderstood the concept of classes. Is this the first time you are using classes in c++? – Ahmed Akhtar Jun 23 '16 at 06:49
  • That would be accurate, yes. I was using them for simple void things until now to cut down on filesize. – Eris Jun 23 '16 at 06:54
  • @DeiniolL Where were all these variables before you tried to introduce the class `inventory`? `maxhealth, maxmana, health, mana, level, agility, strength, healthpotions, manapotions, armourlevel, weaponlevel, crystals, gold, rock, wood`. – Ahmed Akhtar Jun 23 '16 at 06:54
  • They were all located in the main class. Would there be any way to do something similar to what I was intending, or is it a lost cause? – Eris Jun 23 '16 at 06:58
  • @DeiniolL No there is hope, but first explain to me clearly what were you trying to do by putting them in a class? – Ahmed Akhtar Jun 23 '16 at 07:00
  • I was hoping to be able to use a method and call them from a single source, instead of having the inventory text repeated several times throughout the class. – Eris Jun 23 '16 at 07:03
  • @DeiniolL What do you mean by inventory text? The display logic of these variables? – Ahmed Akhtar Jun 23 '16 at 07:05
  • By that I meant the console that display the inventory of the player. – Eris Jun 23 '16 at 07:15
  • I'll be unable to respond any more tonight, a storm just downed my net. Sorry! – Eris Jun 23 '16 at 07:25
  • @DeiniolL Ok I will edit my answer to include some basic explanation of classes and suggest a way to use it in your scenario please have a look at it later. – Ahmed Akhtar Jun 23 '16 at 07:33
  • @AhmedAkhtar I think that might have fixed everything. Thanks! ^^ I'll try and use this method of using classes in the future, as it seems to be something I can incorporate in multiple places. – Eris Jun 23 '16 at 14:07
  • I was wrong about it fixing everything, but that fixed the vast bulk. Only two errors remain so far, although these two errors are duplicated a few times. `src\zoria.o:zoria.cpp:(.text+0x120): multiple definition of inventory::printinv()' src\inventory.o:inventory.cpp:(.text+0x80): first defined here` – Eris Jun 23 '16 at 14:18
  • Update: Changing the include from inventory.cpp to inventory.h seems to have fixed it. – Eris Jun 23 '16 at 14:25
  • Ahh! yes I forgot to tell you about the `.h` instead of `.cpp`, I too found about that [here](http://stackoverflow.com/questions/30142066/how-to-link-cpp-and-h-files-together). Glad that it is finally solved. – Ahmed Akhtar Jun 24 '16 at 05:44
0

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.

Jean Catanho
  • 326
  • 7
  • 18
  • Declare and define, in C++, are two different concepts. In this case, you want "declare" for the last instance of "define" – Nic Jun 23 '16 at 05:10
0

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.

Torbjörn
  • 5,512
  • 7
  • 46
  • 73
  • This has fixed all of them aside from the "'exampleint' was not declared in this scope" error, which is still a vast improvement. – Eris Jun 23 '16 at 05:06
  • Please, read up on member access levels in C++ (i.e. `public`, `protected`, `private`). – Torbjörn Jun 23 '16 at 05:10