Thread: static character help

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    44

    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

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.
    Last edited by Daved; 04-02-2006 at 07:15 PM.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    44
    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

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    44
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  2. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  3. Static variables and functions problem in a class
    By earth_angel in forum C++ Programming
    Replies: 16
    Last Post: 09-15-2005, 12:08 PM
  4. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM