I had a thread going not too long ago, and from that I was able to make a global vector that I can store class objects in. But now when I try to access it, it says "no match for 'operator[]' in 'jailHouse[0]' "

Anyways, Here's the code, if you run it, it should say the same thing:

/* ISU program created by Matt
   in grade 12 programming. */
#include <iostream>
#include <list>
#include <iterator>
#include <vector>
using namespace std;
class humanPlayer
     {
     public:                                                             
          humanPlayer(string thePlayersName, unsigned int thePlayersTurnNum); 
          ~humanPlayer(){};  
 
          void addToList(unsigned int countryToAdd) {countriesOwned.push_back(countryToAdd);} 
          void showList();
          void removeFromList(unsigned int countryToRemove) {countriesOwned.remove(countryToRemove);}
          void checkIfInList(unsigned int countryToCheck);
 
          string getPlayerName() {return playerName;}  
          int getPlayerTurnNum() {return playerTurnNum;}
          void setPlayerTurnNum(unsigned int newPlayerTurnNum);
 
     private:
          list<int> countriesOwned;
          string playerName;
          unsigned int playerTurnNum;
     };
     humanPlayer::humanPlayer(string thePlayersName, unsigned int thePlayersTurnNum)
          {
          playerName = thePlayersName;
          playerTurnNum = thePlayersTurnNum;
          }
 
     void humanPlayer::checkIfInList(unsigned int countryToCheck)
          {
          if(find(countriesOwned.begin(), countriesOwned.end(), countryToCheck) != countriesOwned.end())
               {
               cout << countryToCheck << " is in the list.\n";
               }
          else
               {
               cout << countryToCheck << " is NOT in the list.\n";
               }
          }
 
     void humanPlayer::showList()
          {
          copy(countriesOwned.begin(),countriesOwned.end(),ostream_iterator<int>(cout,", "));
          cout << endl;
          } 
 
     void humanPlayer::setPlayerTurnNum(unsigned int newPlayerTurnNum)
          {
          playerTurnNum = newPlayerTurnNum;
          }
 
int getName();
unsigned int numOfPlayers;
list<humanPlayer> jailHouse;
 
int main()
     {
     getName();
 
     cout << jailHouse[0].getPlayerName() << endl;
 
     system("PAUSE");
     }  
 
int getName()
     {
     cout << "How many players will there be? ";
     cin >> numOfPlayers;
     cout << endl;
 
     for(unsigned int i=0 ; i<numOfPlayers ; i++)
          {
          char name[256];
          cin.ignore(255,'\n');
          cout << "What is player " << (i+1) << "'s name? ";
          cin.get(name,256);
 
          jailHouse.push_back(humanPlayer(name,(i+1)));
          } 
     }

>list<humanPlayer> jailHouse;
jailHouse is a list, the list class doesn't allow random access with the subscript operator.

>if(find(countriesOwned.begin(), countriesOwned.end(), countryToCheck) != countriesOwned.end())
I'm guessing this is giving you problems too. ;)

actually the second thing is giving me no problems what so ever and works perfectly. I'm just wondering how do i access the individual classes in the vector once I put them in there.

>I'm just wondering how do i access the individual classes in the
>vector once I put them in there.

What vector? I don't see any vectors.

> What vector? I don't see any vectors.
I guess he meant the list.

list<humanPlayer>::iterator iter;
for(iter = jailHouse.begin(); iter != jailHouse.end(); ++iter)
{
    //now dereference the iterator and you get access to your instance
   cout << iter->getPlayerName();
   cout << (*iter).getPlayerTurnNum();
}

Thank you! I need to finish something impressive for my isu by mid next week, and although i won't be able to finish the game entirely, I will be able to show off some impressive coding. This was the first major hurdle I hit.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.