views:

253

answers:

4

Hi everyone!

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!

A: 

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.

Leahn Novash
I disagree this is a design problem. This is very common.
gatorfax
A: 

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;

};
TokenMacGuy
Wow... instant downvote. Would you mind sharing with us what it is about this or other answers that you dont like?
TokenMacGuy
+1, that's what I would do too
Clement Herreman
Your code will not compile because you missed one important point.
Pavel Shved
I didn't downvote - but you also need to move the implementation of the addBlock function outside the class definition.
gatorfax
You should have down'ed this. Having such code with at least 4 upvoted shows lack of C++ programming skill among SO users.
Pavel Shved
Clement Herreman
-1 again. This code has several problems, incorrect arguments, inline definitions which cannot work, etc.
gimpf
@pavel: er... what????
AndreasT
I think he answered the question, even if the code contained unrelated errors; so did it really deserve an up/down vote war? Why not just point out the issue and let him improve it (or just remove the unnecessary detail)? (voted neither way BTW).
Clifford
The errors were not unrelated. And this was even accepted... just ridiculous!
Pavel Shved
I don't really care to fix this particular issue. I've marked it community wiki, fix it yourselves.
TokenMacGuy
+5  A: 
  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).

Pavel Shved
+9  A: 

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...
gatorfax