1

I came from Java to C++ ...

When I try to do this ...

class Box {
    Table* onTable;
};

class Table {
    Box* boxOnIt;
};

int main() {
    Table table;
    Box box;

    table.boxOnIt = &box;
    box.onTable = &table;

    return 0;
}

the compiler tell me that Table is undefined. If I switch class definitions the compiler tell me that Box is undefined

In java I was able to do something like this with no problem. Is there a solution for this to work? thanks...

Mustafa
  • 931
  • 1
  • 13
  • 26
  • I find it interesting that no answer has mentioned that you should declare your properties public if you need to access them from outside the class. Class members are implicitly private in C++, and thus you should get a compiler error from the code when trying to access`boxOnIt` or `onTable`. – Kleist Dec 16 '10 at 17:29

7 Answers7

2

you should use forward declarations. just mention this as your first statement:

class Table;  // Here is the forward declaration
Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
  • 1
    and make it habit that you create a seperate (header) file for each object. box.h and table.h and include them in the main.cpp – RvdK Dec 16 '10 at 10:52
2

Add this before class Box:

class Table;

Thus you forward declare class Table so that pointer to it can be used in Box.

Septagram
  • 9,425
  • 13
  • 50
  • 81
2

You have a circular dependency here and need to forward to declare one of the classes:

// forward declaration
class Box;

class Table
{
    Box* boxOnit;
}  // eo class Table

class Box
{
    Table* onTable
} // eo class Box

Note that, generally speaking, we'd have a separate header file for Box and Table, using forward declarations in both, e.g:

box.h

class Table;

class Box
{
    Table* table;
}; // eo class Box

table.h

class Box;

class Table
{
    Box* box;
};  // eo class Table

Then, include the necessary file in our implementation (.cpp) files:

box.cpp

#include "box.h"
#include "table.h"

table.cpp

#include "box.h"
#include "table.h"
Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
1
class Table;

class Box {
    Table* onTable;
};

class Table {
    Box* boxOnIt;
};

int main() {
    Table table;
    Box box;

    table.boxOnIt = &box;
    box.onTable = &table;

    return 0;
}
Igor
  • 26,650
  • 27
  • 89
  • 114
1

You should forward declare one of the two classes:

class Table; // forward declare Table so that Box can use it.

class Box {
    Table* onTable;
};

class Table {
    Box* boxOnIt;
};

int main() {
    Table table;
    Box box;

    table.boxOnIt = &box;
    box.onTable = &table;

    return 0;
}

or viceversa:

class Box; // forward declare Box so that Table can use it.

class Table {
    Box* boxOnIt;
};

class Box {
    Table* onTable;
};

int main() {
    Table table;
    Box box;

    table.boxOnIt = &box;
    box.onTable = &table;

    return 0;
}
Simone
  • 11,655
  • 1
  • 30
  • 43
1

Use forward declarations so that the class declared first knows about the second one. http://www.eventhelix.com/realtimemantra/headerfileincludepatterns.htm

cromestant
  • 652
  • 2
  • 10
  • 21
0

Add class definition on the top

class Table;

class Box {
    Table* onTable;
};

class Table {
    Box* boxOnIt;
};
DReJ
  • 1,966
  • 15
  • 14