两个相互引用的类

时间:2021-08-10 12:56:28

I'm new to C++, so this question may be basic:

我是C ++的新手,所以这个问题可能是基本的:

I have two classes that need to refer to each other. Each is in its own header file, and #include's the other's header file. When I try to compile I get the error "ISO C++ forbids declaration of ‘Foo’ with no type" for one of the classes. If I switch things so the opposite header gets parsed first I get the same error with the other class.

我有两个需要互相引用的类。每个都在自己的头文件中,#include是另一个头文件。当我尝试编译时,我得到错误“ISO C ++禁止声明'Foo'没有类型”的一个类。如果我切换东西,所以首先解析相反的头,我得到与另一个类相同的错误。

Is it possible in C++ to have two classes that need references to each other?

在C ++中是否有可能有两个需要相互引用的类?

For more detail: I have an "App" class and a "Window" class. App needs to refer to Window to make the window. Window has a button that calls back to App, so it needs a reference to App. If I can't have two classes refer to each other, is there a better way to implement this?

更多细节:我有一个“App”类和一个“Window”类。应用程序需要引用Window来创建窗口。 Window有一个回调App的按钮,因此需要引用App。如果我不能让两个类互相引用,有没有更好的方法来实现它?

3 个解决方案

#1


You can use forward declarations in the header files to get around the circular dependencies as long as you don't have implementation dependencies in the headers. In Window.h, add this line:

只要标题中没有实现依赖项,就可以在头文件中使用前向声明来绕过循环依赖项。在Window.h中,添加以下行:

class App;

In App.h, add this line:

在App.h中,添加以下行:

class Window;

Add these lines before the class definitions.

在类定义之前添加这些行。

Then in the source files, you can include the headers for the actual class definitions.

然后在源文件中,您可以包含实际类定义的标头。

If your class definitions reference members of the other class (for example, in inlines), then they need to be moved to the source file (no longer inline).

如果类定义引用其他类的成员(例如,在内联中),则需要将它们移动到源文件(不再是内联)。

#2


Forward declaration is the way to go.

前瞻性宣言是要走的路。

If you are using pointers\reference in class header then Forward declaration at both sides would work for you.

如果您在类标题中使用指针\引用,那么双方的转发声明将适合您。

If you are creating the object as a class member then you must include header itself. ( Forward declaration won't work as compiler needs class definition for knowing the size).

如果要将对象创建为类成员,则必须包含标题本身。 (前向声明不起作用,因为编译器需要类定义来了解大小)。

Refer C++ FAQ for solving such senario:

请参阅C ++ FAQ来解决此类问题:

If you are creating the Window as member then include the Window header in App but at the same time Window shouldn't include the App's header. Use the combination of pointer to App and the forward declaration there.

如果要将Window创建为成员,则在App中包含Window标题,但同时Window不应包含App的标题。使用指向App的指针和那里的前向声明。

#3


You need a forward declaration.

你需要一个前瞻声明。

#1


You can use forward declarations in the header files to get around the circular dependencies as long as you don't have implementation dependencies in the headers. In Window.h, add this line:

只要标题中没有实现依赖项,就可以在头文件中使用前向声明来绕过循环依赖项。在Window.h中,添加以下行:

class App;

In App.h, add this line:

在App.h中,添加以下行:

class Window;

Add these lines before the class definitions.

在类定义之前添加这些行。

Then in the source files, you can include the headers for the actual class definitions.

然后在源文件中,您可以包含实际类定义的标头。

If your class definitions reference members of the other class (for example, in inlines), then they need to be moved to the source file (no longer inline).

如果类定义引用其他类的成员(例如,在内联中),则需要将它们移动到源文件(不再是内联)。

#2


Forward declaration is the way to go.

前瞻性宣言是要走的路。

If you are using pointers\reference in class header then Forward declaration at both sides would work for you.

如果您在类标题中使用指针\引用,那么双方的转发声明将适合您。

If you are creating the object as a class member then you must include header itself. ( Forward declaration won't work as compiler needs class definition for knowing the size).

如果要将对象创建为类成员,则必须包含标题本身。 (前向声明不起作用,因为编译器需要类定义来了解大小)。

Refer C++ FAQ for solving such senario:

请参阅C ++ FAQ来解决此类问题:

If you are creating the Window as member then include the Window header in App but at the same time Window shouldn't include the App's header. Use the combination of pointer to App and the forward declaration there.

如果要将Window创建为成员,则在App中包含Window标题,但同时Window不应包含App的标题。使用指向App的指针和那里的前向声明。

#3


You need a forward declaration.

你需要一个前瞻声明。