简体   繁体   English

为什么前向声明不能解决循环依赖关系?

[英]Why does a forward declaration not fix the circular dependency?

I have three classes: a TopClass which contains an instance of a BottomClass pointer. 我有三个类:一个TopClass ,它包含一个BottomClass指针的实例。 The BottomClass contains a pointer to a HelperClass . BottomClass包含一个指向一个HelperClass The HelperClass keeps a pointer to the TopClass . HelperClass保持一个指针TopClass Circular dependency pops up and a forward declaration is needed in the HelperClass . 循环依赖项弹出,并且在HelperClass需要前向声明。

All of this is illustrated with the following code snippets: 以下代码段说明了所有这些:

#include "BottomLevel.h"

namespace foo
{
  class TopLevel
  {
  private:
    BottomLevel* item;
  };
}

- --

#include "HelperClass.h"

namespace foo
{
  class BottomLevel
  {
  private:
    HelperClass* item;
  };
}

- --

class TopLevel; // forward declaration here

namespace foo
{
  class HelperClass
  {
  public
    HelperClass(TopLevel* item);
  };
}

The issue comes when trying to do things in the implementation file. 尝试在实现文件中执行操作时会出现问题。 If I #include "TopClass.h" in the cpp file, I get compilation errors stating "overloaded member function not found -- use of undefined type ' TopLevel '" (ERRORS C2511 and C2027). 如果我在cpp文件中#include "TopClass.h" ,则出现编译错误,指出“找不到重载的成员函数-使用未定义的类型' TopLevel '”(错误C2511和C2027)。

Then, if I don't do the #include I'm still left with C2027 errors because I try to use the forward declared type. 然后,如果我不执行#include ,则仍然会遇到C2027错误,因为我尝试使用正向声明的类型。

I just know there is a method to fix this, I'm sure I've done it before, but I can't for the life of me remember what I'm supposed to do. 我只知道有一种方法可以解决此问题,我确定我之前已经做过,但是我无法终生记住我应该做的事情。 Any help please? 有什么帮助吗?

The problem is that you're forward declaring TopLevel outside the foo namespace so the compiler is never finding the class foo::TopLevel . 问题是您要在foo命名空间之外向前声明TopLevel ,因此编译器永远不会找到foo::TopLevel类。

Try moving the forward declaration of TopLevel inside the foo namespace. 尝试在foo名称空间中移动TopLevel的前向声明。

You need a forward declaration of the form 您需要表格的前向声明

namespace foo {
class TopLevel;
class BottomLevel;
class HelperClass;
}

Then the order wont matter [because you use pointers] 然后顺序就无关紧要了(因为您使用了指针)

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

相关问题 前向声明和循环依赖 - Forward declaration & circular dependency 为什么前向声明和指针(或引用?)可以解决循环依赖关系? - Why forward declaration and pointer (or reference?) can resolve circular dependency? 循环依赖结构,使用前向声明时对结构进行错误重新定义 - Circular dependency struct, error redefinition of struct when using forward declaration 两个类的前向声明会在构造函数中导致循环依赖吗? - Will forward-declaration of two classes lead to circular dependency in the constructor? 如何使用带有boost :: msm的前向声明来避免循环依赖? - How to use forward declaration with boost::msm to avoid circular dependency? C ++通函#include,其中前向声明不固定 - C++ Circular #include where forward declaration is not fix 前向声明是否修复了参数的数量? - Does forward declaration fix the number of argument? C ++为什么此前向声明失败? - c++ why does this forward declaration fail? 为什么前向声明不适用于类? - Why does forward declaration not work with classes? 循环依赖项(typedef上的类,classdef上的typedef),正向声明给出了模糊的调用? - circular dependency (class on typedef, typedef on class), forward declaration gives ambigous call?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM