Nicolas Risso Nicolas Risso - 1 year ago 68
C++ Question

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.

class Foo {
class Bar;

class Bar {
Foo::Node createNode();

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'

Answer Source

You need to declare the inner class before referencing it:

class Foo {
    class Node;

    // ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download