I'm using gcc versión 4.1.2 (Ubuntu 4.1.2-0ubuntu4). I'm trying to compile this relatively simple code, but it doesn't compiles. The compiler says (i translated some phrases from spanish to english): javimena@javimena-desktop:~/cppexamples$ g++ Example1.cc Example1.cc: In constructor ‘XRIVarArray<Float>::XRIVarArray(int)’: Example1.cc:37: error: ‘x’ was not declared in this scope The only file is Example1.cc: -------------------------------------------------------- // line: 1 #include <cstdio> template<class Float> class XRIVar { public: Float min; Float max; }; template<class Var> class VarArray { protected: Var* x; public: VarArray() { x = 0L; } VarArray(int n) { x = new Var[n]; } Var& operator [](int n) { return x[n]; } }; template<class Float> class XRIVarArray : public VarArray< XRIVar<Float> > { public: XRIVarArray(int n) : VarArray< XRIVar<Float> >(n){ for (int i=0; i < n; i++) { x[i].min = 2; // line: 37 x[i].max = 10; } } }; int main() { using namespace std; XRIVarArray<double> xva(5); printf( "%g\n", xva[1].max); return 0; } -------------------------------------------------------- If I inherit from a class without template arguments, the there is no problem. The workaround for now is to replace x[i] with this->x[i]. But I think this is still a bug.
> The workaround for now is to replace x[i] with this->x[i]. This is not just a workaround but the correct fix. the reason is because x is not dependent so it has to be looked up at definition time and not instantation time (and definition time does not take into account depdent inherited classes). See http://gcc.gnu.org/gcc-3.4/changes.html for more info.