Im trying to do one of the Solutions from the textbook that is one here and I ca
ID: 3813850 • Letter: I
Question
Im trying to do one of the Solutions from the textbook that is one here and I can't get the program to run because of these errors that I dont quite understand.
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
COMPUTERPLAYER.H
#ifndef COMPUTERPLAYER_H
#define COMPUTERPLAYER_H
namespace guessinggame
{
class ComputerPlayer : public Player
{
virtual int getGuess();
};
}
#endif
COMPUTERPLAYER.CPP
#include
#include
#include "computerplayer.h"
using namespace std;
namespace guessinggame
{
int ComputerPlayer::getGuess()
{
int guess;
guess = rand() %100;
cout<<"The computer guess "<< guess << endl;
return guess;
}
}
HUMANPLAYER.H
#ifndef HUMANPLAYER_H
#define HUMANPLAYER_H
#include "player.h"
namespace guessinggame
{
class HumanPlayer : public Player
{
public:
virtual int getGuess();
};
}
#endif
HUMANPLAYER.CPP
#include<iostream>
#include "humanplayer.h"
using namespace std;
namespace guessinggame
{
int HumanPlayer::getGuess()
{
int guess;
do
{
cout<<"Enter a guess (0-99):" <<endl;
cin>>guess;
}
while((guess<0) || (guess>99));
return guess;
}//end getGuess
}}
these are the files that I have been having trouble with.
the biggest one I dont understand is this one
"player.cpp:5:24: error: definition of 'int guessinggame::Player::getGuess()' is not in namespace enclosing 'guessinggame::Player' [-fpermissive] int Player::getGuess()"
Explanation / Answer
#include <iostream>
#include <cstdlib>
#define MAX_VALUE 65536
using namespace std;
/* category Node */
class Node
;
/* category ThreadedBinarySearchTree */
class ThreadedBinarySearchTree
builder */
ThreadedBinarySearchTree()
/* perform to clear tree */
void makeEmpty()
/* perform to insert a key */
void insert(int key)
else if (p->key > key)
else
}
Node *tmp = new Node();
tmp->key = key;
tmp->rightThread = tmp->leftThread = true;
if (p->key < key)
facet */
tmp->right = p->right;
tmp->left = p;
p->right = tmp;
p->rightThread = false;
}
else
}
/* perform to look for a part */
bool search(int key)
come false;
tmp = tmp->right;
}
else if (tmp->key > key)
come false;
tmp = tmp->left;
}
else
come true;
}
}
}
/* Fuction to delete a part */
void Delete(int key)
else if (dest->key > key)
else
}
Node *target = dest;
if (!dest->rightThread && !dest->leftThread)
2 children*/
p = dest;
/* realize largest node at left kid */
target = dest->left;
whereas (!target->rightThread)
/* exploitation replace mode*/
dest->key = target->key;
}
if (p->key >= target->key)
else if (target->rightThread)
{
Node *largest = target->left;
whereas (!largest->rightThread)
largest->right = p;
p->left = target->left;
}
else
{
Node *smallest = target->right;
whereas (!smallest->leftThread)
smallest->left = target->left;
p->left = target->right;
}
}
else
else if (target->rightThread)
{
Node *largest = target->left;
whereas (!largest->rightThread)
largest->right = target->right;
p->right = target->left;
}
else
{
Node *smallest = target->right;
whereas (!smallest->leftThread)
smallest->left = p;
p->right = target->right;
}
}
}
/* perform to print tree */
void printTree()
{
Node *tmp = root, *p;
for (;;)
{
p = tmp;
tmp = tmp->right;
if (!p->rightThread)
{
whereas (!tmp->leftThread)
}
if (tmp == root)
break;
cout<<tmp->key<<" ";
}
cout<<endl;
}
};
/* Main Contains Menu */
int main()