简体   繁体   English

C ++类成员

[英]C++ class members

I came from Java to C++ ... 我从Java到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. 编译器告诉我Table是未定义的。 If I switch class definitions the compiler tell me that Box is undefined 如果我切换类定义,则编译器会告诉我Box未定义

In java I was able to do something like this with no problem. 在Java中,我可以毫无问题地执行类似的操作。 Is there a solution for this to work? 有解决方案吗? thanks... 谢谢...

you should use forward declarations . 您应该使用前向声明 just mention this as your first statement: 只需将此作为您的第一条声明即可:

class Table;  // Here is the forward declaration

Add this before class Box: 在班级框前添加以下内容:

class Table;

Thus you forward declare class Table so that pointer to it can be used in Box. 因此,您可以向前声明类Table,以便可以在Box中使用指向它的指针。

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, eg: 请注意,通常来说,对于BoxTable ,我们将有一个单独的头文件,在这两个文件中都使用前向声明,例如:

box.h

class Table;

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

table.h table.h

class Box;

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

Then, include the necessary file in our implementation (.cpp) files: 然后,在我们的实现(.cpp)文件中包括必要的文件:

box.cpp box.cpp

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

table.cpp table.cpp

#include "box.h"
#include "table.h"
class Table;

class Box {
    Table* onTable;
};

class Table {
    Box* boxOnIt;
};

int main() {
    Table table;
    Box box;

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

    return 0;
}

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;
}

Use forward declarations so that the class declared first knows about the second one. 使用前向声明,以便第一个声明的类知道第二个。 http://www.eventhelix.com/realtimemantra/headerfileincludepatterns.htm http://www.eventhelix.com/realtimemantra/headerfileincludepatterns.htm

Add class definition on the top 在顶部添加类定义

class Table;

class Box {
    Table* onTable;
};

class Table {
    Box* boxOnIt;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM