在内部类中使用struct时编译错误

Compiling error when using struct within an inner class

本文关键字:编译 错误 struct 内部类      更新时间:2023-10-16

我找不到任何答案,可以帮助我理解为什么以下代码不编译。我在类(FOO)的私人部分中声明结构

class Foo {
public:
    Foo();
    class Bar;
    class Bar {
    public:
        Bar();
        Foo::Node createNode();
    };
private:
     struct Node{
        Node(int d) : data(d) {};
        int data;
     };
};

,编译器会引发以下错误:

.../Foo.h:9:14: error: no type named 'Node' in 'Foo'

您需要在引用它之前声明内部类:

class Foo {
    class Node;
public:
    // ...