Original Post
Hello, Im trying to create a hangman game using classes and such, but I hae come across a problem. On the line below, my compiler says ".push_back" undeclared. Note: Game is not complete. [sad] Thanks for any help. Source:
//------------------------------------------------------------------------------
// Name: Brandon Wall
// Date: 9 - 6 - 06
// Desc: Creating the hangman program
//------------------------------------------------------------------------------
#include <ctime>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
class Player
{
public:
Player(const string TheWord);
~Player();
void SetWord(const string& word);
void SetGuess();
void GetUsed();
void GetSoFar();
void CheckGuess(const string& TheWord);
private:
char m_Guess;
vector<char> * m_Used;
string * m_SoFar;
};
int Menu();
void Pause();
string SetWord();
int main()
{
string TheWord = SetWord();
return 0;
}
//------------------------------------------------------------------------------
// Name: CheckGuess()
// Desc: Checks to see if the users guess is correct
//------------------------------------------------------------------------------
void Player::CheckGuess(const string& TheWord)
{
for(int i = 0; i != TheWord.size(); ++i)
{
if(m_Guess == TheWord)
{
m_SoFar = m_Guess;
}
if(TheWord.find(m_Guess) == string::npos)
{
m_Used.push_back(m_Guess); // ERROR RIGHT HERE
}
}
}
//------------------------------------------------------------------------------
// Name: GetUsed()
// Dsc: Prints out the letters that have been guessed
//------------------------------------------------------------------------------
void Player::GetUsed()
{
for(vector<char>::const_iterator cIter = m_Used.begin(); cIter != m_Used.end(); ++cIter)
{
cout << " " << *cIter << " ";
}
}
//------------------------------------------------------------------------------
// Name: GetGuess()
// Desc: Gets the users guess
//------------------------------------------------------------------------------
void Player::SetGuess()
{
cout << "Please enter a letter guess: ";
cin >> m_Guess;
}
//------------------------------------------------------------------------------
// Name: Menu()
// Desc: Gets the user's desired action
//------------------------------------------------------------------------------
int Menu()
{
int menu= 0;
cout << "Welcome to Hangman\n"
<< "Programed by Brandon Wall\n\n"
<< "Menu Items:\n\n"
<< "0 = Quit\n"
<< "1 = New Game\n\n"
<< "Menu Selection: ";
cin >> menu;
return menu;
}
//------------------------------------------------------------------------------
// Name: Pause()
// Desc: Waits for the user to continue
//------------------------------------------------------------------------------
void Pause()
{
cout << "\nPress [ENTER] to continue.";
cin.ignore();
cin.get();
}
//------------------------------------------------------------------------------
// Name: Player()
// Desc: Allocates memry on the heap for the private vectors
//------------------------------------------------------------------------------
Player::Player(const string TheWord)
{
m_Used = new vector<char>;
m_SoFar = new string(TheWord);
}
//------------------------------------------------------------------------------
// Name: ~Player()
// Desc: Free's all the memory on the heap
//------------------------------------------------------------------------------
Player::~Player()
{
delete m_Used;
delete m_SoFar;
}
//------------------------------------------------------------------------------
// Name: SetWord()
// Desc: Sets the word the user to guess
//------------------------------------------------------------------------------
string SetWord()
{
vector<string> words;
words.push_back("buiness");
words.push_back("computer");
words.push_back("evergreen");
words.push_back("designer");
words.push_back("dynamite");
random_shuffle(words.begin(), words.end());
return words[0];
}