Question

So I've got a Piece class supposed to represent pieces on a board, from which I plan to have two other classes inheriting. I've gotten a lot of problems in doing so, however; here is the relevant code so far.

///
/// PIECE CLASS HERE
/// this is an abstract class from which Barrier and Pawn inherit.

class Piece
{
public:
    Piece(Space* start);
    sf::Shape m_Circle;
protected:
    int m_X;
    int m_Y;
    int m_radius;
    Space* CurrentSpace;
};

Piece::Piece(Space* start):
    m_X(start->GetX()),
    m_Y(start->GetY()),
    m_radius(14),
    CurrentSpace(start)
{}

///
/// BARRIER CLASS HERE
/// these are the white stones that block a player's path

class Barrier : public Piece
{
public:
    Barrier(Space* initial);
    void Move(Space* target, bool isCapturing);
};

Barrier::Barrier(Space* initial)
{
    Piece(initial);
    m_Circle = sf::Shape::Circle((float)m_X, (float)m_Y, (float)m_radius, sf::Color(255, 255, 255));
    Move(initial);
}

void Barrier::Move(Space* target, bool isCapturing)
{
    int xChange = abs(target->GetX() - m_X);
    int yChange = abs(target->GetY() - m_Y);
    m_Circle.Move((float)xChange, (float)yChange);
    CurrentSpace.ToggleOccupied();
    if(!isCapturing)
    {
        (*target).ToggleOccupied();
    }
    CurrentSpace = target;
}

I'm getting loads of errors I don't understand, in particular:

no matching function for call to Piece::Piece()
declaration of 'Piece initial' shadows a parameter
no matching function for call to 'Barrier::Move(Piece&)'
request for member 'ToggleOccupied' in '((Barrier*)this)->Barrier::<anonymous>.Piece::CurrentSpace', which is of non-class type 'Space*'|

Being new to C++, I don't understand what's going wrong with any of this. I tried to build my code analogous to the code I found in the book I used to learn C++, but apparently I've overlooked some subtlety. All the functions that I try to call seem to exist in the appropriate places, and I define them with the same values as in their prototype, I think.

Was it helpful?

Solution

The first error is caused by this:

Barrier::Barrier(Space* initial)
{
    Piece(initial);
    m_Circle = sf::Shape::Circle((float)m_X, (float)m_Y, (float)m_radius, sf::Color(255, 255, 255));
    Move(initial);
}

Needs to look like:

Barrier::Barrier(Space* initial) : Piece(initial)
{
    m_Circle = sf::Shape::Circle((float)m_X, (float)m_Y, (float)m_radius, sf::Color(255, 255, 255));
    Move(initial);
}

The base class' constructor runs before your constructor (no matter what) -- you need to pass arguments to it if it requires arguments in the initialization list.

I'm not sure about the other errors because I don't know which lines they're occurring on.

OTHER TIPS

The parenthesis in the line

Piece(initial);

are ignored by the compiler. You are declaring a variable with the same name as the parameter:

Piece initial;

To initialize the Piece base object with initial, you have to use the member initializer list:

Barrier::Barrier(Space* initial) : Piece(initial)

Also, the Move function expects two arguments, but you're only passing one. You forgot the boolean.

Good answers, all. Plus that final daunting line

request for member 'ToggleOccupied' in '((Barrier*)this)->Barrier::<anonymous>.Piece::CurrentSpace', which is of non-class type 'Space*'|

is being caused from

CurrentSpace.ToggleOccupied();

you have declared CurrentSpace to be a pointer, so this should be

CurrentSpace->ToggleOccupied();

If you want to initialize the super class, you should do it like this:

Barrier(Space* initial): 
    Piece(initial) {
    ...
}

If you don't explicitly initialize the base type, the compiler will try to initialize it by calling a constructor with an empty argument list, like this:

Barrier(Space* initial):
    Piece() {
    ... 
}

But since you don't define a constructor for Piece that takes zero arguments, you get the compiler error you described.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top