virtual function being called instead of implementation

Jan 30, 2016 at 6:51pm
so i am making a chess board .
non gui , just the logic.
I have a mother class called piece , and the sub classes (rook ,queen...)which inherit from it.
the board is an 8x8 vector of type piece. in the mother class i have a virtual function called checkLegality which is implemented differently in all classes . the problem i have is that , when i try to call check legality , the virtual version gets called
here is the code.
piece class (mother)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class piece
{
private :
    bool alive   ;
public :

    type pType ;
    color Color  ;
    int xCoor ;
    int yCoor ;
piece ()

{

alive = true;
}

    virtual bool checkLegality (int x , int y  , std::vector <std::vector <piece>> board  ) {};




    bool isAlive ()
    {
        return alive;

    }

    void kill ()
    {
        alive = false;
    }

};


this is the implementation in rook class

1
2
3
4
5
6
bool checkLegality (int x , int y  , std::vector <std::vector <piece>> board  )
    {
        //rules

    }
 

and here is where the function is called

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 void newMove (int x , int y , type pieceType  , color pieceColor )
    {
        vector<piece*> potentialPieces ;
        findPotentials(pieceType ,pieceColor , potentialPieces );
         cout << potentialPieces.size()<< potentialPieces[0]->Color <<potentialPieces[0]->pType<< "\n";

       for (int i = 0 ; i < potentialPieces.size() ;i++)
        {

            if (potentialPieces[i]->checkLegality ( x , y , board ))
            {
                board [potentialPieces[i]->yCoor] [potentialPieces[i]->xCoor] .kill();
                potentialPieces[i]->xCoor = x ;
                potentialPieces[i]->yCoor = y ;

                board [potentialPieces[i]->yCoor] [potentialPieces[i]->xCoor] = *potentialPieces[i];

            }
        }
    }
Last edited on Jan 30, 2016 at 6:52pm
Jan 30, 2016 at 6:57pm
It all depends on what potentialPieces contains. If they're all piece* then piece::checkLegality will be called.
Jan 30, 2016 at 7:02pm
well in the case i have , it should contain a rook, and when i call potentialPieces[0]->pType , it gives the value of a rook . but piece::checkLegality is called anyway .
Last edited on Jan 30, 2016 at 7:03pm
Jan 30, 2016 at 8:42pm
I know what you're saying, but you may have coded something else. That's usually the case.

You haven't shown the code that populate potentialPieces. I mentioned it in my previous post, hoping you'd post it, but you haven't.

The truth is, unless we see all the relevant code, we can't help.
Last edited on Jan 30, 2016 at 8:42pm
Jan 30, 2016 at 8:58pm
virtual bool checkLegality (int x , int y , std::vector <std::vector <piece>> board ) {}

This concerns me. The board variable here can never hold any type other than piece. If newMove is operating on the same variable type, it will never deal with anything other than a piece, regardless of the type indicated by the pType member (and, I have to ask, if you're keeping track of the type already what's the point in the virtual function?)

Jan 30, 2016 at 9:50pm
this is the function that populates the pottential pieces vector
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    void potentialPieces (type _pType , color _pColor , std::vector <piece*> &v  ){
   for(int i = 0 ; i < 8 ; i++)
        {

            for(int j = 0 ; j < 8 ; j++)
            {
                if(board[i][j]->pType ==_pType &&board[i][j]->Color == _pColor)
                {

                    v.push_back(board[i][j]);

                }

            }

        }
    }
Jan 30, 2016 at 10:59pm
Please address my last post. You seem to have multiple variables with the same name (board) and different types and it's rather confusing.

line 16 in the last snippet in the OP would seem to result in object slicing (if, indeed, there were any object other than a piece pointed to by the elements in potentialPieces.)
Last edited on Jan 30, 2016 at 11:00pm
Feb 1, 2016 at 6:44am
You shouldn't mix relying on inheritance mechanisms and using member data directly. It's a contradiction.

Anyway, you still haven't shown where those pointers are created. You don't seem to get that that is the key to determining how a virtual function call is determined.
Topic archived. No new replies allowed.