Accessing elements in classes

I don't understand classes very well, but I'm trying to implement them into my project, because they have many benefits. How can I do something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>

using namespace std;

class Entity
{
public:
    int health;
	int maxhealth;
};

class Hero : public Entity
{
public:
    Hero(string _name) : Entity()
	{
        string name = _name;
		health = 20;
		maxhealth = 20;
	}

};

int main()
{
    cout << "Hero... What is your name?" << "\n";
	string name;
	getline(cin, name);
	Hero player(name);

    cout << "Health: " << Hero::health << "/" << Hero::maxhealth;
    return 0;
}


This code gives the errors:

main.cpp: In function 'int main()':
main.cpp:9:9: error: invalid use of non-static data member 'Entity::health'
main.cpp:32:33: error: from this location
main.cpp:10:6: error: invalid use of non-static data member 'Entity::maxhealth'
main.cpp:32:56: error: from this location
You should use member access operator ".": cout << "Health: " << Hero.health << "/" << Hero.maxhealth;
Thank you for replying, MiiNiPaa, but your fix gives the errors:
main.cpp: In function 'int main()':
main.cpp:32:31: error: expected primary-expression before '.' token
main.cpp:32:53: error: expected primary-expression before '.' token
The 'non-static' part of your error means that the fields health and maxhealth are associated with Hero OBJECTS, like player, not the class Hero itself.

cout << "Health: " << player.health << "/" << player.maxhealth;
Last edited on
Then this gives the errors:
C:\DOCUME~1\Vidminas\LOCALS~1\Temp\ccAXYCuT.ltrans0.ltrans.o:ccAXYCuT.ltrans0.o:(.text.startup+0xd1): undefined reference to `Entity::health'
C:\DOCUME~1\Vidminas\LOCALS~1\Temp\ccAXYCuT.ltrans0.ltrans.o:ccAXYCuT.ltrans0.o:(.text.startup+0xdb): undefined reference to `Entity::maxhealth'
C:\DOCUME~1\Vidminas\LOCALS~1\Temp\ccAXYCuT.ltrans0.ltrans.o:ccAXYCuT.ltrans0.o:(.text.startup+0xf4): undefined reference to `Entity::maxhealth'
C:\DOCUME~1\Vidminas\LOCALS~1\Temp\ccAXYCuT.ltrans0.ltrans.o:ccAXYCuT.ltrans0.o:(.text.startup+0xfd): undefined reference to `Entity::health'
You need to have an instance of the class and access its members using the '.' operator.

For example, here's a class. It defines a user-defined type called 'Entity':
1
2
3
4
5
6
class Entity
{
public:
    int health;
    int maxhealth;
};


Think of the class definition as a blue print for an object. Just as you would not put water into the blue print for a bucket, you do not store data inside the type. You have to instantiate the class to create an object first. Extending the previous analogy, you have to manufacture a bucket from its blue print before you have anything to put water in.

This will instantiate the class, creating the e object or instance.
Entity e;

Now you can use e, like this:
1
2
    e.health = 10;
//  ^ note that here, you were trying 'Entity' not the instance 

Try a clean and rebuild.
You appear to have separated your classes in different files (or at least different than main). The linker cannot find the definition for Entity.

You need to link with the appropriate object (.o) files. Check your project settings, if you're in an IDE. Otherwise, post the command line or makefile and we'll get you straightened out.

This is like trying to solve a puzzle without seeing all of the pieces.

ok that's a bit confusing, but here's what I understood:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>

using namespace std;

class Entity
{
public:
    static int health;
    static int maxhealth;
};

class Hero : public Entity
{
public:
    Hero(string _name) : Entity()
    {
        string name = _name;
	health = 20;
	maxhealth = 20;
    }

};

int main()
{
    cout << "Hero... What is your name?" << "\n";
	string name;
	getline(cin, name);
	Hero player(name);
	Entity hello;

    cout << "Health: " << hello.health << "/" << hello.maxhealth;
    return 0;
}


Which gives me the same errors about Entity::health and Entity::maxhealth as above.
Last edited on
You don't want those members to be static. That will fix the linker error. (Static members need to be initialized, that's the source of the error.)

Also, you already have a 'Hero', so use player.health and player.maxhealth and remove hello. By the way, line 18 isn't helping you out any because it declares a local variable named name. You probably wanted that in class Hero (similar to class Entity).
I removed the static and everything is good now (I forgot I had changed that, sorry :P).

Line 18 was just an extract from the whole code, I accidentally included it into my example, it should be there, or at least I think so :D.

Thank you everyone!
Topic archived. No new replies allowed.