Question

I'm having some problems with my class because they both depends on each other, to one can't be declared without the other one being declared.

class block: GtkEventBox {

    public:
        block(board board,guint x,guint y): image("block.png") {
            this.board = board;
            this.x = x;
            this.y = y;
            board.attach(this,x,y,x+1,y+1);
        }
        void move(guint x,guint y) {
            board.remove(this);
            this.x = x;
            this.y = y;
            board.attach(this,x,y,x+1,y+1);
        }

    private:
        guint x, y;
        board board;
        GtkImage image;

};

class board: Gtk::Table {

    public:
        board(): Gtk::Table(25,20) {
            blocks_c = 0;
        }
        void addBlock(guint x,guint y) {
            blocks_a[blocks_c++] = new block(this,x,y);
        }

    private:
        block* blocks_a[24];
        int blocks_c;

};

As you can see the "block" class needs to know what a "board" is and vice versa. Thanks in advance!

Was it helpful?

Solution

Define "board" before "block" and forward declare the "block" class. Also, move the implementation of the board functions out of the class definition.

// forward declare block class
class block;

// declare board class
class board: Gtk::Table {

    public:
        board();
        void addBlock(guint x,guint y);

    private:
        block* blocks_a[24];
        int blocks_c;

};

// declare block class
class block: GtkEventBox {

    public:
        block(board board,guint x,guint y);
        void move(guint x,guint y);

    private:
        guint x, y;
        board board;
        GtkImage image;

};

// define member functions (implementation) here...

OTHER TIPS

  1. Forward-declare your block class before board with this line:

    class block;

  2. Place the code of the function bodies AFTER declarations of both classes. Forward-declaring your class doesn't make all its functions available, it just allows the compiler to know that such class exists. It just allows to use, for instance, pointers to such a class (because the size of pointer type doesn't depend on the layout of the class).

This can easily be resoved with a forward declaration. Since board doesn't need to know anything much about the block class, only that it uses a pointer to it, declare it first. but before that include a forward declaration for block. It looks like this:

class block; // <- Forward declaration!

class board: Gtk::Table {

    public:
        board(): Gtk::Table(25,20) {
            blocks_c = 0;
        }
        void addBlock(guint x,guint y) {
            blocks_a[blocks_c++] = new block(this,x,y);
        }

    private:
        block* blocks_a[24];
        int blocks_c;

};    class block: GtkEventBox {

    public:
        block(board board,guint x,guint y): image("block.png") {
            this.board = board;
            this.x = x;
            this.y = y;
            board.attach(this,x,y,x+1,y+1);
        }
        void move(guint x,guint y) {
            board.remove(this);
            this.x = x;
            this.y = y;
            board.attach(this,x,y,x+1,y+1);
        }

    private:
        guint x, y;
        board board;
        GtkImage image;

};

This is usually a design problem. I suggest you to pick the smaller class (in your case I'd suggest the block class) and code some events on it that the board class would sign. Then instead of calling the board class method, shoot the event and let the board class call it itself.

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