如何转发声明内部类?

发布于 2024-07-25 12:04:01 字数 653 浏览 6 评论 0原文

我有一个像这样的类...

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 类的头文件?

I have a class like so...

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

    ...
};

Elsewhere, I want to pass a Container::Iterator by reference, but I don't want to include the header file. If I try to forward declare the class, I get compile errors.

class Container::Iterator;

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

Compiling the above code gives...

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

How can I forward declare this class so I don't have to include the header file that declares the Iterator class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吃素的狼 2024-08-01 12:04:02

我知道没有办法完全按照你想要的方式做,但如果你愿意使用模板,这里有一个解决方法:

// 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 的并实现导出; ))。

I know of no way to do exactly what you want, but here is a workaround, if you are willing to use templates:

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

Hopefully, this idiom might be of some use to you (and hopefully your compiler is EDG-based and implements export ;) ).

離人涙 2024-08-01 12:04:01

这根本不可能。 您不能在容器外部转发声明嵌套结构。 您只能在容器内转发声明。

您需要执行以下操作之一:

  • 使类成为非嵌套类
  • 更改声明顺序,以便首先完全定义嵌套类
  • 创建一个公共基类,该基类既可以在函数中使用,也可以由嵌套类实现。

This is simply not possible. You cannot forward declare a nested structure outside the container. You can only forward declare it within the container.

You'll need to do one of the following

  • Make the class non-nested
  • Change your declaration order so that the nested class is fully defined first
  • Create a common base class that can be both used in the function and implemented by the nested class.
苏佲洛 2024-08-01 12:04:01

我不相信在不完整的类上向前声明内部类有效(因为如果没有类定义,就无法知道是否确实存在内部类)。 因此,您必须在 Container 的定义中包含一个前向声明的内部类:

class Container {
public:
    class Iterator;
};

然后在相应的源文件中,实现 Container::Iterator:

class Container::Iterator {
};

然后 #include 仅容器标头(或者不担心前向声明,只包含两者) )

I don't believe forward declaring inner class of on an incomplete class works (because without the class definition, there is no way of knowing if there actually is an inner class). So you'll have to include in the definition of Container, a forward declared inner class:

class Container {
public:
    class Iterator;
};

Then in the corresponding source file, implement Container::Iterator:

class Container::Iterator {
};

Then #include only the container header (or not worry about forward declaring and just include both)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文