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?
#include <iostream>
#include <string>
usingnamespace 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
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.
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
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.
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).