0

So I am trying to make a chess game ( bit of an ultimate challenge for me ), and I'm a stump for this part ..

So I made a piece object, and the idea is that in the main game code, I have an array of pieces and I pass the address of the array to the function "InitilisePieces" and the team ( Black or White ) and it shall assign all the pieces. So I made the function a friend to access all the private members and it comes up with an error saying "inaccessible", and I don't understand what's wrong with doing as I've done. Any help would be more than appreciated!

Side Note: things like State_ and _Location and structs and enums that are defined properly etc, not the problem ... ( I don't think )

Header File Contains:

class   __Piece
{
private:

    State_              e_state;
    Piece_Type_         e_type;
    Team_               e_team;
    _Location           st_location;

    friend void         InitilisePieces     ( __Piece(*)[16], Team_);

public:
    __Piece             ();

};

.cpp File Contains:

void                    InitilisePieces     ( __Piece * pao_piece[16], Team_ )
{
    int         n_count;

    for ( n_count = 0; n_count < 16; n_count++ )
    {
        pao_piece[ n_count ]->e_state;
    }
}

UPDATE:

Thankyou for the explanations and I get where I'm going wrong now ... so what is the parameter supposed to be to pass the address of an array of __Piece 's?

user3502489
  • 321
  • 1
  • 4
  • 11
  • 8
    [Do not begin your identifiers with underscores](http://stackoverflow.com/a/224420/1938163) – Marco A. Oct 14 '15 at 11:30
  • As an aside, `__Piece` and `_Location` are reserved identifiers. – emlai Oct 14 '15 at 11:30
  • I think that getters and setters are much more suitable than friend function here – David Haim Oct 14 '15 at 11:30
  • @MarcoA. Beginning with an underscore is just fine, as long as the next character is not uppercase or another underscore, and the identifier is not in global scope. – emlai Oct 14 '15 at 11:31
  • I like it, and I've never had a problem with it. and its not causing this error. – user3502489 Oct 14 '15 at 11:32
  • 4
    Not saying that it is the cause of your problem but it's a discouraged practice (read more from the link I posted). – Marco A. Oct 14 '15 at 11:32
  • @DavidHaim .. Getter Setters would be fine (ish, it would add more code ) and that was my next step, but regardless why doesn't ^^ that work – user3502489 Oct 14 '15 at 11:33

1 Answers1

7

Your friend function and the function you define later have the same name but different signatures. You have not defined the friend function.

This

void InitilisePieces( __Piece(*)[16], Team_);

is not the same as this

void InitilisePieces( __Piece * pao_piece[16], Team_ )

The former's first parameter is a pointer to an array of 16 __Pieces. The latter's first parameter is adjusted to __Piece** pao_piece, i.e a pointer to a pointer to a __Piece. In other words, it is this:

void InitilisePieces( __Piece** pao_piece, Team_ )

Also: watch out for reserved identifiers.

Community
  • 1
  • 1
juanchopanza
  • 216,937
  • 30
  • 383
  • 461