2010-06-14 53 views
0

我想在類模板(內部類)內定義的類之間使用繼承。但是,編譯器(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)。有人有想法嗎?

+1

[問題模板繼承]的複製(http://stackoverflow.com/questions/2982660/problem-with -template-inheritance)(簡短版本是:它不是一個bug,它只是名稱查找在C++中的作用) – 2010-06-14 01:52:51

回答