Im trying to figure out what this error means for the code I\'m trying to run. I
ID: 3814038 • Letter: I
Question
Im trying to figure out what this error means for the code I'm trying to run. Its a C++ program.
I don't quite understand what this error means, but I think im missing an #include or something in Player.cpp im just not sure.
This is one of the solutions from the textbook I just can't get it to run
"player.cpp:5:24: error: definition of 'int guessinggame::Player::getGuess()' is not in namespace enclosing 'guessinggame::Player' [-fpermissive] int Player::getGuess()"
MAIN.CPP
#include
#include "humanplayer.h"
#include "player.h"
#include "computerplayer.h"
using namespace std;
using namespace guessinggame;
void play(Player &player1, Player &player2);
bool checkForWin(int guess, int answer);
int main()
{
ComputerPlayer player1;
ComputerPlayer player2;
play(player1,player2);
system("pause");
return (0);
}
bool checkForWin (int guess, int answer)
{
if (answer == guess)
{
cout<<"You're right! You win!"< return 0;
}
else if (answer cout <<"Your guess is too high"< else
cout <<"Your guess is too low"< return false;
}
void play(Player &player1, Player &player2)
{
int answer=0, guess=0;
answer=rand()%100;
bool win =false;
while(!win)
{
cout <<"Player 1's turn to guess."< guess=player1.getGuess();
win=checkForWin (guess,answer);
if(win)return;
cout<<"Player 2's turn to guess."< guess=player2.getGuess();
win=checkForWin (guess,answer);
}
}}
PLAYER.H
#ifndef PLAYER_H
#define PLAYER_H
namespace guessinggame
{
class Player
{
public:
virtual int getGuess();
};
#endif
PLAYER.CPP
#ifndef PLAYER_H
#define PLAYER_H
namespace guessinggame
{
class Player
{
public:
virtual int getGuess();
};
#endif
Explanation / Answer
namespace usually define a package in layman terms. Even if you remove namespace statements, and directly include the the helper files, your program will work.