我试图在类模板(内部类)中定义的类之间使用继承 . 但是,编译器(GCC)拒绝让我访问基类中的公共成员 .
示例代码:
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;
编译器说:
file.cpp: In member function ‘int& Space::PlaneX::at(int, int)’:
file.cpp:21: error: ‘space’ was not declared in this scope
如果将 using Plane::space;
添加到类PlaneX的定义中,或者通过 this
指针访问基类成员,或者如果将类Space更改为非模板类,则编译器可以正常使用它 .
我不知道这是对C的一些模糊限制,还是GCC中的一个错误(GCC版本4.4.1和4.4.3测试) . 有没有人有想法?
1 回答
它应该是与c的两阶段名称查找相关的问题:
http://gcc.gnu.org/onlinedocs/gcc-4.4.3/gcc/Name-lookup.html#Name-lookup