Inheritance inside a template - public members become invisible?

Posted by Juliano on Stack Overflow See other posts from Stack Overflow or by Juliano
Published on 2010-06-14T01:48:51Z Indexed on 2010/06/14 1:52 UTC
Read the original article Hit count: 244

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about c++

Related posts about inheritance