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
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.
#include <iostream> //input/output
#include <string> //strings
#include "player.h" //header for player
#include "menu.h"//header for menus
usingnamespace 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;
}
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.
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.
#ifndef PLAYER_H // if Person class is not defined
#define PLAYER_H // then define (multiple definitions will cause problems)
#include <iostream>
#include <string>
usingnamespace 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
#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"
usingnamespace 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
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.
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.
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