Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++类成员 我从java到C++……/p>_C++_Class_Members - Fatal编程技术网

C++类成员 我从java到C++……/p>

C++类成员 我从java到C++……/p>,c++,class,members,C++,Class,Members,当我尝试这样做的时候 class Box { Table* onTable; }; class Table { Box* boxOnIt; }; int main() { Table table; Box box; table.boxOnIt = &box; box.onTable = &table; return 0; } 编译器告诉我该表未定义。 如果我切换类定义,编译器会告诉我框未定义 在java中,我可以毫

当我尝试这样做的时候

class Box {
    Table* onTable;
};

class Table {
    Box* boxOnIt;
};

int main() {
    Table table;
    Box box;

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

    return 0;
}
编译器告诉我该表未定义。 如果我切换类定义,编译器会告诉我框未定义

在java中,我可以毫无问题地做类似的事情。 有没有解决办法? 谢谢…

您应该使用。这是你的第一句话:

class Table;  // Here is the forward declaration
你应该使用。这是你的第一句话:

class Table;  // Here is the forward declaration

在类框之前添加此项:

class Table;

因此,您可以对表进行分类,以便指向它的指针可以在框中使用。

在分类框之前添加以下内容:

class Table;
class Table;

class Box {
    Table* onTable;
};

class Table {
    Box* boxOnIt;
};

int main() {
    Table table;
    Box box;

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

    return 0;
}

因此,您可以对表进行分类,以便在框中使用指向它的指针。

您应该向前声明两个类中的一个:

class Table;

class Box {
    Table* onTable;
};

class Table {
    Box* boxOnIt;
};

int main() {
    Table table;
    Box box;

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

    return 0;
}
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;
}
或反之亦然:

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

您应该向前声明两个类中的一个:

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;
}
或反之亦然:

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

在顶部添加类定义

class Table;

class Box {
    Table* onTable;
};

class Table {
    Box* boxOnIt;
};

在顶部添加类定义

class Table;

class Box {
    Table* onTable;
};

class Table {
    Box* boxOnIt;
};

这里有一个循环依赖项,需要转发以声明其中一个类:

// forward declaration
class Box;

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

class Box
{
    Table* onTable
} // eo class Box
注意,一般来说,Box和Table都有一个单独的头文件,在这两个文件中都使用前向声明,例如:

方框h

表h

然后,在我们的实现.cpp文件中包括必要的文件:

box.cpp

表1.cpp


这里有一个循环依赖项,需要转发以声明其中一个类:

// forward declaration
class Box;

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

class Box
{
    Table* onTable
} // eo class Box
注意,一般来说,Box和Table都有一个单独的头文件,在这两个文件中都使用前向声明,例如:

方框h

表h

然后,在我们的实现.cpp文件中包括必要的文件:

box.cpp

表1.cpp


使用前向声明,以便第一个声明的类知道第二个声明。

使用前向声明,以便第一个声明的类知道第二个声明。

并养成习惯,为每个对象创建一个单独的头文件。h和table.h,并将它们包含在main.cpp中,并使您习惯于为每个对象创建单独的头文件。box.h和table.h,并将它们包含在main中。cpp这是您需要在顶部的声明,而不是定义。这是您需要在顶部的声明,而不是定义。我发现有趣的是,没有答案提到,如果您需要从类外访问属性,则应将其声明为公共。类成员在C++中是隐式的私有的,因此当尝试访问Access BoxOnIT或OnTable时,应该从代码中获得编译器错误。我发现有趣的是,没有回答提到如果您需要从类外部访问它们,则应该声明您的属性为公共。类成员在C++中隐含私有,因此当尝试访问Access BoxOnIT或OnTable时,应该从代码中获得编译器错误。