-
static character help
i cant get my static character to work right. i start the first player off at 'A' which i get fine but when it goes to the next player i want the player to be 'B' and so on for the number of players casue there can be more than 2 players. but after the 'A' gets printed the second player is not 'B' it just prints some wierd character instead.
class to make characters
Code:
class Player
{
private:
char symbol;
public:
static char players;
Player() {symbol = players++;}
~Player();
char getMark() {return symbol;}
bool operator == (Player& player);
};
Player::~Player(){}
bool Player::operator == (Player& player)
{
return symbol == player.symbol;
}
// Global
char Player::players = 'A';
class to hold # of players
Code:
class Playerlist
{
private:
int size;
Player* player; // array
public:
Playerlist();
~Playerlist();
Playerlist (const Playerlist& playerlist);
int getSize() {return size;}
};
Playerlist::Playerlist()
{
player = NULL;
cout << "How many players will be playing? " << endl;
cout << "* Number of players should not exceed the cubic dimensions * " << endl;
cin >> size;
}
Playerlist::~Playerlist()
{
delete [] player;
}
Playerlist::Playerlist (const Playerlist& playerlist)
{
size = playerlist.size;
player = new Player [size];
for(int s = 0; s < size; s++)
player[s] = playerlist.player[s];
}
this is a switch statement you hin enter to mark the place you want on the board
.
Code:
.
.
.
case ENTER:
//if(array[currentDepth][currentRow][currentCol].mark(player))
// cout << "Space already taken choose again" << endl;
//else
array[currentDepth][currentRow][currentCol].mark(player);
player++;
break;
so in short ststic character starts at A and for each player in game it gets incramented ie: player2 = B player 3 = C etc. but it dont do that i get a different character when its incramented
-
Where do you actually add Players to your Playerlist? In the posted code, you set the size in the regular constructor, but you never allocate space for the player array. Later, when you call the copy constructor, you are copying bad information from uninitialized memory.
You need the call to new Player[size] in the default constructor.
-
so i should put "player = new Player [size]" in the defalt constructor is what ur saying right? i tried that and it still does the same thing. i put it right after i prompt for the size
-
Could you explain or post the code that is having the problem. I don't understand what the switch code has to do with anything. Nothing in that code seems to match your classes. You do need that line in your default constructor, so leave it in there.
Perhaps you could create a simple main() that uses your class but doesn't do what you expect.
-
actually i figured out my problem i made a new player in my main and never removed it which was causeing problems and i added player=new Plyayer[size] in the default constructor but thx for helping