Nested class' function: implementation code

Hi C++ folks,

I can implement a function inside a nested class as follows:
1
2
3
4
5
class A {
  struct B {
   void f() { //impl code here; }
  };
};


or with this:
1
2
3
4
5
6
7
8
9
class A {
  struct B {
   void f(); // declaration only
  };
};

struct A::B {  
   void f() { //impl code here; }
};


But I got a compilation error if I try this:
1
2
3
4
5
6
7
class A {
  struct B {
   void f(); // declaration only
  };
};

void A::B::f() { //impl code here; } // compilation error 


How can I get rid of the compilation error of the third approach?
Last edited on
The third one works fine for me (except the commented '}' )
What error are you getting?

But the second one fails. And it should. Struct redefinition is not allowed.
Last edited on
Sorry for the confusion with my original sample code. I got it (the issue is with the return type):
1
2
3
4
5
6
class A {
  struct C;  // defined later
  struct B {
   C f(); // declaration only
  };
};


instead of:
 
C A::B::f() { //impl code here; } // compilation error 


It should be:
 
B::C  A::B::f() { //impl code here; } // return type: B::C 


BWT( @hamsterman): the second one should be:
1
2
3
4
5
class A {
   struct B;
};

struct A::B { // imp code here }; 
Last edited on
C is not part of B, it's part of A

therefore it should be:

1
2
3
4
A::C A::B::f()
{
 // ...
}
A typo.It should be:
 
A::C  A::B::f() { //impl code here; } // return type: A::C  

Last edited on
That's correct (except for the commented out closing brace, which you seem to keep doing in your examples)

So... what's the problem? What compilation error are you getting?
The problem solved. To summarize:
1
2
3
4
5
6
class A {
  struct C;  // defined later
  struct B {
   C f(); // declaration only
  };
};

I got an compilation error "return type is not defined" when I used:
 
C A::B::f() { // impl } 

The correct one should be:
 
A::C A::B::f() { // impl } 
Last edited on
Topic archived. No new replies allowed.