Calling base functions in derived classes

Alright, I've looked through my book and I haven't really found much help, I'm trying to get player::print() to go off in my menu::print() or at least player::getname() but it returns this error menu.cpp(8) : error C2352: 'player::getName' : illegal call of non-static member function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "menu.h"//include class header file
#include <string>

using namespace std;

void menu::print() const
{
	cout << "Hello " << &player::getName();
	cout << " " << endl;
	cout << "Hit the corresponding key to your choice and hit enter." << endl;
	cout << "1 Play." << endl;
	cout << "2 Quit." << endl;
	cout << "3 Save." << endl;
	cout << "4 Load." << endl;
}
menu::menu()
{
}


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
#include "player.h"//include class header file
#include <string>

using namespace std;

void player::print() const//function to print name
{
	cout << "Player name is " << playName << " ";
}
void player::setName(string player1)//function to set name
{
	playName = player1;
}
string player::getName() const//function to get name
{
	return player1;
}
void player::winloss() const//function to print win/loss
{
	cout << win << " " << loss << endl;
}
void player::playerName(string playName)
{
	string player1 = playName;
}
player::player()//default constructor
{
	win = 0;
	loss = 0;
	player1 = " ";
	playName = " ";
}
The way you are wording suggests that you are having player be derived from menu, which makes no sense from my point of view...You will need to pass a player to menu::print() and use that.
Nah, I've fixed that, and I meant that menu is derived from player(the class was to originally just make a class, I chose player and then the instructor said to create menu derived from the class we chose before), but I need to have the menus print function have a hello (call to get name in main.cpp here) and it's returning an uninitialized name variable.

this is my menu implementation file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "menu.h"//include class header file
#include "player.h"//include class header file
#include <string>

using namespace std;

void menu::print() const
{

	player playerType;
	cout << "Hello " << 
	cout << playerType.getName() << endl;
	cout << "Hit the corresponding key to your choice and hit enter." << endl;
	cout << "1 Play." << endl;
	cout << "2 Quit." << endl;
	cout << "3 Save." << endl;
	cout << "4 Load." << endl;
}
menu::menu()
{
}


this is my player implementation file.

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
#include "player.h"//include class header file
#include <string>

using namespace std;

void player::print() const//function to print name
{
	cout << "Player name is " << playName << " ";
}
void player::setName(string player1)//function to set name
{
	playName = player1;
}
string player::getName() const//function to get name
{
	return player1;
}
void player::winloss() const//function to print win/loss
{
	cout << win << " " << loss << endl;
}
void player::playerName(string playName)
{
	string player1 = playName;
}
player::player()//default constructor
{
	win = 0;
	loss = 0;
	player1 = " ";
	playName = " ";
}


this is main

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
#include <iostream> //input/output
#include <string> //strings
#include "player.h" //header for player
#include "menu.h"//header for menus

using namespace std;

int main()
{
cout << "Welcome please enter your name." << endl;//greeting
string name;//variable for input of player name
player playerType;//player name object
cin >> name;//input of player name
playerType.setName(name);//function to set player name
playerType.print();//function to print players name
cout << " " << endl;//space to seperate menu

menu menuType;//menu object
menuType.print();//showing menu

char choice;//variable to hold menu choice
cin >> choice;//input of menu choice

while (choice != '2')//loop to end when the user chooses to quit
{
	switch (choice)//menu
	{
		case '1':
			cout <<	"Play goes here" << endl;
			break;
		case '2':
			cout << "Thanks for playing!" << endl;
			break;
		case '3':
			cout << "Save function goes here." << endl;
			break;
		case '4':
			cout << "Load function goes here." << endl;
			break;
		default:
			cout << "Invalid selection." << endl;
	}

menuType.print();//showing menu
cin >> choice;//input of menu choice

}

return 0;

}


I hope that was more clear.
Ok, I think I get what you are saying now. If you define the same function in the derived class, it will overwrite the previous definition you gave it in the base class. Otherwise, you can simply call the base class's function like it was your own.
Not sure how to call the function without an object.
I'm not seeing any inheritance here. Can you show your menu and player declarations? If you're inheriting player in menu, you shouldn't create an instance of player in your print method. Menu has inherited all the members and methods of player already. So you can call menu.setName() and menu.getName(). In menu::print(), just call getName() without any 'prefix'.

In main, don't declare a player at all. You only need to declare a menu, since it has inherited everything from player. This is really a pretty improper use of inheritance since menu and player are conceptually completely different, but if that is the required assignment, that's what you have to do.

Your manipulation of the playername in your player methods doesn't appear correct either, but it's hard to say exactly what incorrect without seeing the class declaration.
Ok, all the examples in the book have them in the same .cpp/h files so it's hard to know how to do it right in seperate ones, I had assumed it was just to include the others header files but here they are.

player header

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
#ifndef PLAYER_H // if Person class is not defined

#define PLAYER_H // then define (multiple definitions will cause problems) 

#include <iostream>
#include <string>

using namespace std;

class player
{
private:
string playName;//variable to hold place players name in player1
string player1;//variable to hold players name

protected:
int win;//variable to hold win count
int loss;//variable to hold loss count

public:
void print() const;//function to print players name
void setName(string player1);//function to set player names
string getName() const;//function to get names
void winloss() const;//function to print win/loss
void playerName(string playName);
player();//default constructor
};

#endif // end the compiler directive for the define  



menu header

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef MENU_H // if menu class is not defined

#define MENU_H // then define (multiple definitions will cause problems) 

#include <iostream>
#include <string>
#include "player.h"

using namespace std;

class menu
{
private:

public:
void usemenu();//function to use menu
void print() const;//function to print menu
menu();//default constructor
};

#endif // end the compiler directive for the define 


sorry, i'm probably just stpid
Alright, tried to do menu : public player
and then call player::getName();
but it's still just giving me an unitialized variable.
Your file arrangement looks ok though you shouldn't use namespaces in your header files (but that's a separate topic). I just wanted to see your header files to see how your classes are defined.

1. You aren't inheriting anything in your menu class.
Read up on the basics of inheritance here: http://cplusplus.com/doc/tutorial/inheritance.html

2. What is the difference between the two strings in your player class? You're also declaring a local string player1 in your PlayerName() method, and I don't think that was your intention. You're assigning the passed in parameter to that string, but it's gone as soon as that call returns.
nevermind, it's giving nothing, i just noticed that it was a problem with one of the couts << giving me the variable
I was trying to use playName as the input and insert that into player1 and player1 into playName for the players name, I don't remember why I think i was having a problem using just one variable before, but I don't remember, i'll look at it and try it with one right now. //edit oh, i didn't notice one was a string and the other wasn't woops
Last edited on
not sure what I did but now i can't get anything x.x
I got it finished now, thanks for the help, sorry I was so hardheaded and not understanding it.
Topic archived. No new replies allowed.