Simple class problem

My codes below, i'm basically trying to get my head around inheritance and classes, and objects.
so i was hoping to create two types of 'player'. and then be able to call functions which show the differences in .. the players basically haha

but for some reason this isn't working.
On my 'class Warrior' line, i get the error "return type may not be specified on constructor"
and in my main.cpp
my Mage.displayStats();
says it's inaccessible.

Help with this would be great!
Thanks!

This is my player.h
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
#include <string>
#include <iostream>
using namespace std;

class player{
public:
	string player_name;
	int health;
	int magic;
	int strength;

	void displayStats();
}

void displayStats(){
	cout <<"Magic:" << endl;
	cout << player.magic << endl;
	cout <<"Stregth:" << endl;
	cout << player.strength << endl;
	cout <<"Health:" << endl;
	cout << player.health << endl;
}

class Mage : player {
public:
	Mage();
}

Mage::Mage(){
	health = 7;
	strength = 10;
	magic = 2;
}

class Warrior : player {
public:
	Warrior();
}

Warrior::Warrior(){
	health = 7;
	strength = 10;
	magic = 2;
}


and my main.cpp will look something like this (havent thought what im guna do afterwards, but this is enough for now)

1
2
3
4
5
6
7
8
9
10
#include <iostream> 
#include "player.h"

int main(){
	Mage mageObject;
	Mage.displayStats();

	system("pause");
	return 0;
}

class definition syntax has to be like below:

class ABC {
...
};
Ah thank you for that, an embarrassingly easy fix.
But i'm still uncertain with this problem

1
2
3
4
5
6
7
8
void displayStats(){
	cout <<"Magic:" << endl;
	cout << player.magic << endl;
	cout <<"Stregth:" << endl;
	cout << player.strength << endl;
	cout <<"Health:" << endl;
	cout << player.health << endl;
}


.magic
.strength
.health
they are all highlighted with the error which is

'error: a nonstatic member reference must be relative to a specific object'
You have displayStats() as a member function of your player class but what you have actually implemented is an ordinary function which will have global scope. You need to use the scope resolution operator to tell the compiler your are implementing a member function.

1
2
3
4
5
void player::displayStats(){
    .
    .
    .
}


Now once you are inside the function you are inside the player class scope and you can use the other member variables directly:

cout << magic << endl;

This is equivalent to using the this pointer (which isn't necessary here):

cout << this->magic << endl;

Similarly for the other output lines.

Also, putting using namespace std in a header file pulls in the entire std namespace and hence every time you #include "player.h" the entire std namespace comes along for the ride. Eventually this will lead to a conflict of names of identifiers. In the header file it is best to leave out the using statement and fully qualify the names, i.e., std::string, std::cout, std::endl.
That's excellent!
thank you for the detailed explanation, it really explains a lot!
I see were i was going wrong now
and thank you for letting me know about the headers, very good to know :)

Thanks again as well Eric Du!
Topic archived. No new replies allowed.