简体   繁体   English

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

[英]Compiling error when using struct within an inner class

I could not find any answer that helped me understand why the following code does not compile. 我找不到任何有助于我理解以下代码为何无法编译的答案。 Im declaring a struct within the private part of a class (Foo), and trying to use it from within an inner class (Bar) like this. 我在类的私有部分(Foo)中声明了一个结构,并尝试从内部类(Bar)中使用这种结构。

class Foo {
public:
    Foo();
    class Bar;

    class Bar {
    public:
        Bar();
        Foo::Node createNode();
    };

private:
     struct Node{
        Node(int d) : data(d) {};
        int data;
     };
};

And the compiler throws the following error: 并且编译器将引发以下错误:

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

You need to declare the inner class before referencing it: 您需要在引用内部类之前对其进行声明:

class Foo {
    class Node;
public:

    // ...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM