首页 文章

我如何转发声明内部类? [重复]

提问于
浏览
132

可能重复:在C中转发嵌套类型/类的声明

我有一个这样的课......

class Container {
public:
    class Iterator {
        ...
    };

    ...
};

在其他地方,我想通过引用传递一个Container :: Iterator,但我不想包含头文件 . 如果我尝试转发声明类,我会遇到编译错误 .

class Container::Iterator;

class Foo {
    void Read(Container::Iterator& it);
};

编译上面的代码给出了......

test.h:3: error: ‘Iterator’ in class ‘Container’ does not name a type
test.h:5: error: variable or field ‘Foo’ declared void
test.h:5: error: incomplete type ‘Container’ used in nested name specifier
test.h:5: error: ‘it’ was not declared in this scope

我怎样才能转发声明这个类,所以我不必包含声明Iterator类的头文件?

3 回答

  • 114

    这根本不可能 . 您无法转发在容器外部声明嵌套结构 . 您只能在容器中转发声明它 .

    您需要执行以下操作之一

    • 使类非嵌套

    • 更改声明顺序,以便首先完全定义嵌套类

    • 创建一个公共基类,既可以在函数中使用,也可以由嵌套类实现 .

  • 1

    我不相信在一个不完整的类工作中声明内部类(因为没有类定义,没有办法知道是否确实存在内部类) . 因此,您必须包含Container的定义,并使用前向声明的内部类:

    class Container {
    public:
        class Iterator;
    };
    

    然后在单独的头文件中,实现Container :: Iterator:

    class Container::Iterator {
    };
    

    然后#include只包含容器 Headers (或者不担心前向声明并且只包括两者)

  • 20

    我知道无法完全按照您的意愿行事,但如果您愿意使用模板,这是一种解决方法:

    // Foo.h  
    struct Foo
    {
       export template<class T> void Read(T it);
    };
    

    // Foo.cpp
    #include "Foo.h"
    #include "Container.h"
    /*
    struct Container
    {
        struct Inner { };
    };
    */
    export template<> 
      void Foo::Read<Container::Inner>(Container::Inner& it)
    {
    
    }
    

    #include "Foo.h"
    int main()
    {
      Foo f;
      Container::Inner i;
      f.Read(i);  // ok
      f.Read(3);  // error
    }
    

    希望这个成语可能对你有用(希望你的编译器是基于EDG的并实现导出;)) .

相关问题