Vector elp Needed Please! (C++)

Started by
6 comments, last by NUCLEAR RABBIT 17 years, 7 months ago
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];
}




Advertisement
What's the exact compiler error you are getting? Try words.push_back(string("buiness")); maybe that will help.
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)
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();


what you need is m_Used->push_back();
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 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();


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]
Quote:Original post by NUCLEAR RABBIT
thnax, i didnt see that [smile]


I was hopeing I edited my post fast enough :P next time!
Quote:Original post by kaysik
Quote:Original post by NUCLEAR RABBIT
thnax, i didnt see that [smile]


I was hopeing I edited my post fast enough :P next time!


no problem [smile] thanx for the help man

This topic is closed to new replies.

Advertisement