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)));
}
}