-1
class A {
    class B { }
    B somefunction();
}

B A::somefunction() {
   B bla;
   return bla;
}

So I have something similar to this in my code. And I am getting an error saying that 'B' does not name a type. Any help would be appreciated. P.S. I am not allowed to change the interface.

David G
  • 94,763
  • 41
  • 167
  • 253
Pure
  • 85
  • 1
  • 8
  • Because B is not visible outside A. – Nard Dec 15 '14 at 18:15
  • Sorry for the abstraction but this is a part of an assignment so I only needed to clarify some concept. Otherwise it would be immoral. Thank you all for the help. – Pure Dec 15 '14 at 18:26

4 Answers4

4

A leading return type is interpreted outside the scope of the function; so nested types need to be qualified if used there:

A::B A::somefunction() {

Alternatively, since C++11, you could use a trailing return type, which (like function parameter types) is interpreted in the scope of the function:

auto A::somefunction() -> B {
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • I liked that you used trailing return types :D – Nard Dec 15 '14 at 18:18
  • 1
    Might be worth referencing [ADL](http://stackoverflow.com/questions/8111677/what-is-argument-dependent-lookup-aka-adl-or-koenig-lookup)? – matsjoyce Dec 15 '14 at 18:20
  • @matsjoyce: There's no ADL here, since no function is being called. This is qualified lookup in the first example, and unqualified lookup (in a suitable scope) in the second. – Mike Seymour Dec 15 '14 at 18:25
0

B is scoped to A, so in order to refer to B you need use the scope resolution operator: A::B.

So something like A::B A::somefunction() for your definition of somefunction should work.

sdzivanovich
  • 740
  • 3
  • 14
0

B is in the A class, and behaves kindof as if it is in an A namespace. Try:

class A {
    class B { }
    B somefunction();
}

A::B A::somefunction() {
   B bla;
   return bla;
}
IdeaHat
  • 7,641
  • 1
  • 22
  • 53
0

Your definition of somefunction() should look like

 A::B A::somefunction() {
   B bla;
   return bla;
 }
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190