0

I'm trying to use inheritance among classes defined inside a class template (inner classes). However, the compiler (GCC) is refusing to give me access to public members in the base class.

Example code:

template <int D>
struct Space {
    struct Plane {
        Plane(Space& b);
        virtual int& at(int y, int z) = 0;
        Space& space;             /* <= this member is public */
    };

    struct PlaneX: public Plane {
        /* using Plane::space; */
        PlaneX(Space& b, int x);
        int& at(int y, int z);
        const int cx;
    };

    int& at(int x, int y, int z);
};

template <int D>
int& Space<D>::PlaneX::at(int y, int z) {
    return space.at(cx, y, z);  /* <= but it fails here */
};

Space<4> sp4;

The compiler says:

file.cpp: In member function ‘int& Space::PlaneX::at(int, int)’:
file.cpp:21: error: ‘space’ was not declared in this scope

If using Plane::space; is added to the definition of class PlaneX, or if the base class member is accessed through the this pointer, or if class Space is changed to a non-template class, then the compiler is fine with it.

I don't know if this is either some obscure restriction of C++, or a bug in GCC (GCC versions 4.4.1 and 4.4.3 tested). Does anyone have an idea?

Juliano
  • 39,173
  • 13
  • 67
  • 73
  • 1
    Duplicate of [problem with template inheritance](http://stackoverflow.com/questions/2982660/problem-with-template-inheritance) (the short version is: it's not a bug, it's just how name lookup works in C++) – James McNellis Jun 14 '10 at 01:52
  • There are 4 ways to solve this problem: **1)** Use the prefix `Plane::space`, **2)** Use the prefix `this->space`, **3)** Add a statement `using Plane::space`, **4)** Use a global compiler switch that enables the permissive mode. The pros & cons of these solutions are described in https://stackoverflow.com/questions/50321788/a-better-way-to-avoid-public-member-invisibility-and-source-code-bloat-repetitio – George Robinson May 14 '18 at 13:24

1 Answers1

1

It should be a problem related to c++'s two-phase name lookup:

http://gcc.gnu.org/onlinedocs/gcc-4.4.3/gcc/Name-lookup.html#Name-lookup

Baiyan Huang
  • 6,463
  • 8
  • 45
  • 72
  • That's it! This and http://www.parashift.com/c++-faq-lite/templates.html#faq-35.19 (another link found in the mentioned duplicate). Thanks. – Juliano Jun 14 '10 at 02:10