我尝试在类模板(内部类)内定义的类之间使用继承。但是,编译器(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
指针,或者如果类空间更改为非模板类,那么编译器可以使用它。
我不知道这是不是一个模糊的C++限制,还是GCC中的一个错误(GCC版本4.4.1和4.4.3测试)。有人有主意吗?