Juliano
Juliano

Reputation: 41447

Inheritance inside a template - public members become invisible?

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?

Upvotes: 0

Views: 549

Answers (1)

Baiyan Huang
Baiyan Huang

Reputation: 6781

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

Upvotes: 1

Related Questions