Inheritance not working

Hello community,
I am trying to code a simple type game, just to keep myself busy and get better at classes and pointers. I was wondering if anyone could help me understand why this code won't work. I am getting an error in the main, where I say Human myhuman, to declare that variable, but it is not working. If I say Base myhuman, no problems, but then the parameters never get set, any help is useful, thanks.
-Matt-
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int quit;
string input;

// Declare each races function, to be used later.
void Human();
void Gnome();
void Elf();
void Troll();

// Declare basic class setup
class Base
{
private:
	string bRace;
	unsigned int bHealth;
	unsigned int bDamage;
	unsigned int bLevel;
public:
	enum HP
	{
		None = 0,
		Small = 5,
		Average = 7,
		Large = 10
	};
	
	Base()
		: bRace("Not a race"),
		bHealth(0),
		bDamage(0),
		bLevel(0)
	{
	}
	Base(const string& Race, unsigned int Health, unsigned int Damage, unsigned int Level)
		: bRace(Race),
		bHealth(Health),
		bDamage(Damage),
		bLevel(Level)
	{
	}

	// Returning values
	const string& GetRace() const { return bRace; }
	unsigned int GetHealth() { return bHealth; }
	unsigned int GetDamage() { return bDamage; }
	unsigned int GetLevel() { return bLevel; }
};

// Declare the race calsses and parameters
class Human:public Base
{
public:
	Human()
		: Base("Human", Average, 5, 1) {}
};
class Gnome:public Base
{
public:
	Gnome()
		: Base("Gnome", Small, 4, 1) {}
};
class Elf:public Base
{
public:
	Elf()
		: Base("Elf", Average, 6, 1) {}
};
class Troll:public Base
{
public:
	Troll()
		: Base("Troll", Large, 4, 1) {}
};

int main()
{
	cout << "------------------------------\n";
	cout << "Welcome to the mini-adventure!\n";
	cout << "------------------------------\n";
	cout << "First, choose your class:\n";
	Base myclass;
	typedef vector<Base*> BaseVector;
	BaseVector Stats;
	while(quit == 0)
	{
		cout << "0.) Exit\n";
		cout << "1.) Human\n";
		cout << "2.) Gnome\n";
		cout << "3.) Elf\n";
		cout << "4.) Troll\n";
		cin >> input;
		if(input.find("0") == 0)
			break;
		if(input.find("1") == 0)
		{
			Human myHuman;
			Stats.push_back(&myHuman);
			BaseVector::iterator cur, curEnd;
			cur = Stats.begin();
			curEnd = Stats.end();
			cout << "Race: " << (*cur)->GetRace() << "\n";
		}
	}
	return 0;
}
Obligatory link:

http://cplusplus.com/forum/articles/40071/#msg218019


What does "not working" mean? Tell us what error you're getting.
Whatever your problem might be, you can't do this:
1
2
Human myHuman;
Stats.push_back(&myHuman);


myHuman is destroyed at the end of the scope, while the vector keeps a pointer to it.

And you shouldn't store pointers in a vector anyway since it does not call delete on the pointers when destructed.
Use ptr_vector instead (boost has an implementation of it).
syntax error : missing ';' before identifier 'myHuman' is the error code.
Ok Athar, how would I keep myHuman active then, so I can output it's parameters.
That's because you have a declared a function called Human, which has the same name as your class.
As for the other problem, use new to dynamically create a new human, i.e. Stats.push_back(new Human);
If you don't use ptr_vector, you will have to call delete on the contents of Stats manually when you no longer need them.
I'm confused, if i set the Human() inside the class Human to something else, it gives me the error: only a constructor can have a base/member initializer list
Could you potentially include some example code of how to fix this?
I didn't mean the constructor, you need that one.

I mean these lines:
1
2
3
4
5
// Declare each races function, to be used later.
void Human();
void Gnome();
void Elf();
void Troll();
Oh wow, I completely missed that, thanks. I got it working now.
Topic archived. No new replies allowed.