Incomplete type compilation error

Hey guys,

I am trying to build a chess game and decided to create a class called 'board' which contains an 8x8 array of squares and some methods, and a nested class called 'square' which has its own properties and methods and which I only declare in 'board' and define externally as board::square. For now I am just trying to reset each square to have the name "myName" and no piece in it, but in compilation I get the error "squares has incomplete type", any idea as to why? (I prefer not to use pointers if possible).

Thanks!

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
35
36
37
38
39
40
41

const int n = 8;

class board {

    private:
        class square;
        square squares[n][n];
    public:

        void initSquare(int i, int j) {
            string newName = "myName";
            squares[i][j](newName);
        }

        void initBoard() {
            for (int i=0; i<n; i++)
                for (int j=0; j<n; j++) initSquare(i, j);
        }

        board() {
            initBoard();
        }
};

class board::square {

    private:
        string name, occupier;
    public:
        void setName(string newName) { name = newName; }
        void setOccupier(string s) { occupier = s; }
        string getName() { return name; }
        string getOccupier() { return occupier; }
        square(string newName) {
            setName(newName);
            setOccupier("Empty");
        }

};
Last edited on
you can only use forward declarations with pointers and references definition. For anything else you should have full class definition avaliable before its use.

You can make class square completely unrelated to board (not nested class) and place its definition before board, or define square on declaration place (inside board)
 
class square;

This is just a forward declaration of the class. It tells the compiler there is a class named square but not what it consists of. All this will allow you to do is to have pointers and references to the class.

To be able create arrays and object, and call methods of this class you need the full class definition. The board member functions can be moved so that they are defined after the class definition of board::square but the squares array has to be defined inside the board class definition so I see no other option than to move the board::square class definition inside the board class definition (above where you define squares).
Last edited on
First of all thank you very much MiiNiPaa and Peter87.

I've tried moving the definition of the class board::square inside the class board (i.e. as "class square {};") as you suggested Peter87, I've done that before and I am getting the same error I did then: "no match for call to ‘(board::square) (std::string&)'". Is this still related to nesting or a different issue with the constructor?
squares[i][j](newName);You are trying to call operator() on your squares array element, which it lacks. You probably want to call setName function: squares[ i][j]setName(newName);
Oh right! I am using the constructor wrong I just need to use setName() thank you that solved it!
Topic archived. No new replies allowed.