Thread: Dynamically Bind dang you! :)

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    40

    Dynamically Bind dang you! :)

    I am working on a chess program. I have a base class called Piece and then other classes like Pawn, Rook, etc... Problem is that I can't get them to do dynamic binding. Here's the code that needs to do the dynamic binding:

    Code:
    vector<location> Board::possibleMoves(location loc)
    {
    	Piece* temp = &board[loc.row][loc.col];
    	vector<location> listLocs;
    	listLocs = temp->getMoves(board);
    	return listLocs;
    }
    in the piece class it looks like:

    Code:
    virtual vector<location> getMoves(vector<vector<Piece> > board)
    		{			cout<<"pay attention: "<<endl;
    			vector<location> temp;
    			return temp;
    		}
    Finally I want the pawn class to overload this particular function and it looks like:

    Code:
    vector<location> getMoves(vector<vector<Piece> > board)
    		{
    			cout<<"We made it"<<endl;
    			vector<location> temp;
    			if(isBlack)
    			{
    				location loc1;
    				
    				if(row==1)
    				{
    					loc1.row=row;
    					loc1.col=col+1;
    					temp.push_back(loc1);
    					loc1.col=col+2;
    					temp.push_back(loc1);
    				}
    				else
    				{
    					loc1.row=row;
    					loc1.col=col+1;
    					if(loc1.col<=7)
    						temp.push_back(loc1);
    				}
    				if(row+1<=7 && col+1<=7 && board[row+1][col+1].getRow()!=-1)
    				{
    					loc1.row=row+1;
    					loc1.col=col+1;
    					temp.push_back(loc1);
    				}
    				if(row+1<=7 && col-1>=0 && board[row+1][col-1].getRow()!=-1)
    				{
    					loc1.row=row+1;
    					loc1.col=col-1;
    					temp.push_back(loc1);
    				}
    			}
    			else
    			{
    				location loc1;
    				
    				if(row==6)
    				{
    					loc1.row=row;
    					loc1.col=col-1;
    					temp.push_back(loc1);
    					loc1.col=col-2;
    					temp.push_back(loc1);
    				}
    				else
    				{
    					loc1.row=row;
    					loc1.col=col-1;
    					if(loc1.col>=0)
    						temp.push_back(loc1);
    				}
    				if(row-1>=0 && col+1<=7 && board[row-1][col+1].getRow()!=-1)
    				{
    					loc1.row=row-1;
    					loc1.col=col+1;
    					temp.push_back(loc1);
    				}
    				if(row-1>=0 && col-1>=0 && board[row-1][col-1].getRow()!=-1)
    				{
    					loc1.row=row-1;
    					loc1.col=col-1;
    					temp.push_back(loc1);
    				}
    			}
    			return temp;
    		}
    You can see my debug code. It's still in there. When the function is called, it prints "pay attention" instead of "we made it". Any thoughts on what needs to be changed to make this perform dynamic binding?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This has been answered elsewhere.

    *thread closed*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bind() fails with WSAEACCES
    By pheres in forum Tech Board
    Replies: 2
    Last Post: 02-24-2009, 01:58 PM
  2. File I/O problem for dynamically allocated struct array
    By veecee in forum C++ Programming
    Replies: 2
    Last Post: 05-05-2006, 09:28 PM
  3. What does bind() do, exactly?
    By Hunter2 in forum Networking/Device Communication
    Replies: 9
    Last Post: 07-07-2005, 12:12 PM
  4. Bind();
    By SirCrono6 in forum Networking/Device Communication
    Replies: 5
    Last Post: 02-26-2005, 08:01 PM
  5. I need a dynamically allocated linked list
    By Flex in forum C Programming
    Replies: 2
    Last Post: 03-06-2002, 02:28 PM