0

I'm designing a chess game (C++ beginner) however I'm stuck on my checking process for individual piece types, eg pawn moving only 1 space at a time, except the first time moving 2 spaces, and so on.

I have a piece class, and a subclass of that is pawn, king, etc, which contains the method:

check(string position,string destination);

and return a boolean value whether it is possible to move to the destination.

I have a pointer to each piece which I have defined by doing:

pawn pawnPieces[16];
piece *pointer[16];

for (i=0;i<16;i++)
{
    pointer[i]=&pawnPieces[i];
}

After my initial checking, I want to call the check function above from main, as a test:

cout << pointer[1]->check("B1","C1") << endl;

This gives me the error "no member named 'check' in piece" which makes sense, however I'm sure there would be a way to link the piece class to the pawn, king etc.

I think I need to do something with virtual functions from what I have read, but I am not sure how to approach this. If anyone could offer a few pointers that would be much appreciated.

This approach of trying to call the subclass function from a pointer to the class above it may be fundamentally flawed, perhaps I need to modify my design to achieve this goal? I still want to keep the check method of the pawn class in the same position, as I believe it encapsulates it well.

EDIT: I made a pure virtual function in the piece class:

virtual bool check(string positionIN,string destination)=0;

Now when I call the cout line above, I get a segmentation fault and I'm unsure why. I'm assuming it's because I'm using pointers?

EDIT2: Thank you for that! I have implemented this however I got a small error, is virtual meant to be attached to the pawn and king class? From my understanding I thought the virtual tag only goes on the base class.

EDIT3: I understand, I tagged the check function in classes pawn and king with the virtual tag and it compiled.

Now I am getting a segmentation fault through calling the object itself

pawnPieces[1].check("B1","C1") 

and by calling the pointer to the object

pointer[1]->check("B1","C1") 

from main, and I am not sure why.

EDIT4: All working now, I was calling it from main to test, however when I called it from within my program everything worked, thank you all!

3
  • 1
    Well, lookup interfaces and virtual , pure virtual respectively. Commented Oct 23, 2015 at 0:07
  • Virtual/pure methods are definitely the way to go. Declare check() in piece, and override it in pawn, king, etc. If you are still having problems, please show your actual code. Commented Oct 23, 2015 at 0:33
  • Edit 2: It only needs to be in the base class, but I'd prefer using it in all of them (as well as override) to make it more obvious. Commented Jan 27, 2016 at 1:23

1 Answer 1

2

What you are attempting to do is exactly what virtual methods are meant for.

class piece
{
public:
    virtual bool check(string position, string destination) = 0;
};

class pawn : public piece
{
public:
    virtual bool check(string position, string destination)
    {
        return ...;
    }
};

class king : public piece
{
public:
    virtual bool check(string position, string destination)
    {
        return ...;
    }
};
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, I am starting to get somewhere!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.