Error when using a base class member in a class nested in a template in C ++

Consider the following example:

template <typename T>
struct A {
  struct B {
    int b;
  };

  struct C : B {
    void f() {
      b = 0;
    }
  };
};

Compiling with GCC 4.8.1 gives the following error:

test.cc: In member function ‘void A<T>::C::f()’:
test.cc:9:11: error: ‘b’ was not declared in this scope
           b = 0;
           ^

However, it bis a member of the parent class b(I used structin the example to make everything public), and if I do a Anon-template, everything compiles.

Why does the compiler give this error and how to avoid it?

+4
source share
1 answer

This is a kind of obscure regional example in the language, but the solution is simple, qualify it:

this->b = 0; // alternatively 'B::b = 0;'

, b , , . , A<T>::B , , b.

+5

Source: https://habr.com/ru/post/1537522/


All Articles