Thread: easy question about classes but need answer fast

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

    easy question about classes but need answer fast

    so i ned to access a private variable from another class but i can figure out how to. here is the code

    Code:
    class Playerlist
    {
    private:
    
    	int size;
    	Player* player; // array 
    
    public:
    	Playerlist();
    	~Playerlist();
    	Playerlist (const Playerlist& playerlist);
    	int getSize() {return size;}
    
    	Player* getPlayer(int index) {return &player[index];}
    
    };
    
    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;
    	player = new Player [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];
    }
    and this is how i thought you were suppose to do it

    Code:
    int size = size.getSize();
    this is the class im trying to get it into
    Code:
    template <class TYPE>
    bool Grid<TYPE>::winHorizontal(Player* player)	//rows
    {
    	int count;
    	
    
    
    	for(int d = 0; d <DEPTH; d++){
    		for(int r = 0; r<ROW; r++){
    			for(int c = 0; c< COL; c++)
    			{
    				if(*player == *array[d][r][c].getPlayer())
    					count++;
    			}
    			if(count == COL)
    				return true;
    			count = 0;	
    		}
    	}	
    	
    		return false;
    }
    
    template <class TYPE>
    bool Grid<TYPE>::winVertical(Player* player)	//columns
    {
    	int count;
    	for(int c = 0; c <COL; c++){
    		for(int r = 0; r<ROW; r++){
    			for(int d = 0; d < DEPTH; d++)
    		{
    			if(*player == *array[d][r][c].getPlayer())
    				count++;
    		}
    		if(count == ROW)
    			return true;
    		count = 0;
    		}
    	}
    	return false;
    }
    
    template <class TYPE>
    bool Grid<TYPE>::winDiagonal(Player* player)	//diagonals
    {
    	int count;
    	int size = size.getSize();
    	for(int i = 0; i < size; i++)
    	{
    		//cubic diagonal top front left to bottom back right
    		if(*player == *array [i][i][i].getPlayer())	
    			count++;
    
    		//cubic diagonal bottom front left to top back right
    		else if(*player == *array[i][(size-1)-i][i].getPlayer())
    			count++;
    
    		//cubic diagonal top front right to bottom back left
    		else if(*player == *array[i][i][(size-1)-i].getPlayer())
    			count++;
    
    		//cubic diagonal bottom front right to top back left
    		else if(*player == *array[i][(size-1)-i][(size-1)-i].getPlayer())
    			count++;
    	}
    	for(int d = 0; d < DEPTH; d++){
    		for(int i = 0; i < size; i++)
    		{
    		// '\' diagonal check
    		if(*player == *array[d][i][i].getPlayer())
    			count++;
    
    		// '/' diagonal check
    		else if(*player == *array[d][i][(size-1)-i].getPlayer())
    			count++;
    		}
    			
    	}
    			if(count == size)
    				return true;
    
    			count = 0;
    			return false;
    }
    
    template <class TYPE>
    bool Grid<TYPE>::winCubic(Player* player) //depth
    {
    	int count;
    	for(int r = 0; r < ROW; r++){
    		for(int c = 0; c < COL; c++){
    			for(int d = 0; d < DEPTH; d++)
    			{
    				if(*player == *array[d][r][c].getPlayer())
    					count++;
    			}
    			if(count == DEPTH)
    				return true;
    				count  = 0;
    		}
    	}
    	return false;
    }
    without that size my code wont work how can i get the size from playerlist to that grid class function??

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    first off, don't post saying you need an answer fast. It's incredibly bad manners.

    second, post the specific errors you're having and the lines they relate to. no-one wants to read through that much code.

    thirdly in this code
    Code:
    	int size = size.getSize();
    that won't work at all. something like
    Code:
    Playerlist plist;
    int size = plist.getSize();
    finally your playerlist class is reinventing the wheel. use a vector.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    44
    sorry bout the fast thing didnt mean any harm. and yea there probably is a better way of doin everything i do really right now but i dont know vectors yet but thx very much i knew there was a way of doing it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Question on Classes
    By gguy85 in forum C++ Programming
    Replies: 6
    Last Post: 02-14-2005, 02:28 AM
  2. Question about Classes
    By WildFire in forum C++ Programming
    Replies: 8
    Last Post: 06-18-2004, 07:57 PM
  3. Flowchart question: Classes
    By darnok in forum C++ Programming
    Replies: 6
    Last Post: 06-17-2004, 06:25 PM
  4. Replies: 22
    Last Post: 11-08-2001, 11:01 PM
  5. Replies: 2
    Last Post: 11-05-2001, 02:02 PM