not_included
not_included

Reputation: 21

Problems with cross dependency classes

I have problem with this part of code. compiler says that i cannot use pointer to not completed class. I'v already tried to use include Board class in Figure class and Figure in board, but this cause serious problem with compiler, and whole bunch of errors comes out.(#pragma once and/or guards in headers was used )

//Board.h
class Figure;

class Board
{
Figure *sz[8][8];
...
public:
void showRange();
friend class Figure;
};

//-------------------
//Board.cpp

void Board::showRange()
{
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {

            if(sz[j][i])
                sz[j][i]->range();

        }
    }
    this->display();
}
...
//Figure.h
class Board;
class Figure
{
protected:
    Board *s;                       
    int x, y;                               

public:

    virtual void range() = 0;
    friend class Board;

};

//range funcions are defined in member classes 

[edit1] Added figure.h to board.h Compiler Error C2027 in Figure.cpp and member classes.files

Severity Code Description Project File Line Suppression State
Error C2027 use of undefined type 'Board' ..\figure.cpp 7
Error C2027 use of undefined type 'Board' ..\figure.cpp 15
Error C2027 use of undefined type 'Board' ..\figure.cpp 17
Error C2027 use of undefined type 'Board' ..\figure.cpp 25
Error C2027 use of undefined type 'Board' ..\figure.cpp 26
Error C2027 use of undefined type 'Board' ..\bishop.cpp 7
Error C2027 use of undefined type 'Board' ..\bishop.cpp 13
Severity    Code    Description Project File    Line    Suppression State
Error (active)  E0393   pointer to incomplete class type is not allowed ..\Figure.cpp   7   
Error (active)  E0393   pointer to incomplete class type is not allowed ..\Figure.cpp   15  
Error (active)  E0393   pointer to incomplete class type is not allowed ..\Figure.cpp   17  
Error (active)  E0393   pointer to incomplete class type is not allowed ..\Figure.cpp   26  
Error (active)  E0393   pointer to incomplete class type is not allowed ..\Figure.cpp   25

Upvotes: 0

Views: 134

Answers (1)

nivpeled
nivpeled

Reputation: 1838

Board.cpp must #include "Figure.h", otherwise the compiler have no idea of range method (which is invoked in Board.cpp) of Figure object.

By the way: Why do you need the friend statement? it usually suggests poor design.

Upvotes: 3

Related Questions