Sign in to follow this  
NUCLEAR RABBIT

Vector elp Needed Please! (C++)

Recommended Posts

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[i])
             {
                  m_SoFar[i] = 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];
}




Share this post


Link to post
Share on other sites
Quote:
Original post by kaysik
vector<char> * m_Used;

You've created a pointer to a vector of chars, then you try to use it as the object itself with:

m_Used.push_back();

Quick solution: Take the * out of the declaration because I don't think you want it. You want to create the vector itself, not a pointer to it. You can have a pointer but then you'll need to new/delete it and worry about memory allocation etc which wouldn't be any use for something like this.


Im jus using memory allocation because thats what im learning so im trying to use it somewhere in my project [samile]

Also, the error im getting are these:

1) 59 C:\Documents and Settings\Brandon\My Documents\DevC++\Hangman - Classes.cpp `push_back' has not been declared

2) 59 C:\Documents and Settings\Brandon\My Documents\DevC++\Hangman - Classes.cpp request for member of non-aggregate type before '(' token

3) 72 C:\Documents and Settings\Brandon\My Documents\DevC++\Hangman - Classes.cpp `begin' has not been declared

4) 72 C:\Documents and Settings\Brandon\My Documents\DevC++\Hangman - Classes.cpp `end' has not been declared

Please Help!

Share this post


Link to post
Share on other sites
Quote:
Original post by NUCLEAR RABBIT
Quote:
Original post by kaysik
vector<char> * m_Used;

You've created a pointer to a vector of chars, then you try to use it as the object itself with:

m_Used.push_back();

Quick solution: Take the * out of the declaration because I don't think you want it. You want to create the vector itself, not a pointer to it. You can have a pointer but then you'll need to new/delete it and worry about memory allocation etc which wouldn't be any use for something like this.


Im jus using memory allocation because thats what im learning so im trying to use it somewhere in my project [samile]

Also, the error im getting are these:

1) 59 C:\Documents and Settings\Brandon\My Documents\DevC++\Hangman - Classes.cpp `push_back' has not been declared

2) 59 C:\Documents and Settings\Brandon\My Documents\DevC++\Hangman - Classes.cpp request for member of non-aggregate type before '(' token

3) 72 C:\Documents and Settings\Brandon\My Documents\DevC++\Hangman - Classes.cpp `begin' has not been declared

4) 72 C:\Documents and Settings\Brandon\My Documents\DevC++\Hangman - Classes.cpp `end' has not been declared

Please Help!


Quote:
Original post by kaysik
what you need is m_Used->push_back();


Share this post


Link to post
Share on other sites
Quote:
Original post by raz0r
Quote:
Original post by NUCLEAR RABBIT
Quote:
Original post by kaysik
vector<char> * m_Used;

You've created a pointer to a vector of chars, then you try to use it as the object itself with:

m_Used.push_back();

Quick solution: Take the * out of the declaration because I don't think you want it. You want to create the vector itself, not a pointer to it. You can have a pointer but then you'll need to new/delete it and worry about memory allocation etc which wouldn't be any use for something like this.


Im jus using memory allocation because thats what im learning so im trying to use it somewhere in my project [samile]

Also, the error im getting are these:

1) 59 C:\Documents and Settings\Brandon\My Documents\DevC++\Hangman - Classes.cpp `push_back' has not been declared

2) 59 C:\Documents and Settings\Brandon\My Documents\DevC++\Hangman - Classes.cpp request for member of non-aggregate type before '(' token

3) 72 C:\Documents and Settings\Brandon\My Documents\DevC++\Hangman - Classes.cpp `begin' has not been declared

4) 72 C:\Documents and Settings\Brandon\My Documents\DevC++\Hangman - Classes.cpp `end' has not been declared

Please Help!


Quote:
Original post by kaysik
what you need is m_Used->push_back();


thnax, i didnt see that [smile]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this