Folks,
I am pretty new at compiling GCC, so I would like to know if anyone has ever seen something like this. The attached program compiles cleanly under g++ 3.3.2 but fails to compile under g++ 3.4.2 and 3.4.3.
I created a base class that defines an integer as a protected member. Then, from a template class that derives from the previous base class, I can access the protected member in the base class.
If I derive an nother template class from the previously defined one, I cannot access the protected member anymore.
I would appreciate if someone can confirm the problem or tell me what I have done wrong... I hope that all the needed informations can be found in the source file.
Regards,
Stephane Ouellette.
/* * This program demonstrates a problem encountered when * trying to access a protected attribute from derived classes * when these classes are templates. * * This program compiles without any error on g++ 3.3.2 but * refuses to compile under g++ 3.4.2 and g++ 3.4.3 with the * following error message: * * test.cpp: In constructor `DerivedClassThatDoesNotWork<T>::DerivedClassThatDoesNotWork(int, T)': * test.cpp:45: error: `V' undeclared (first use this function) * test.cpp:45: error: (Each undeclared identifier is reported only once for each function it appears in.) * * GCC Configuration * --prefix=/usr --libdir=/usr/lib --with-slibdir=/lib --mandir=/usr/share/man * --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking * --enable-long-long --enable-__cxa_atexit --enable-clocale=gnu * --enable-languages=c,c++,ada,f77,objc,java --host=i586-mandrake-linux-gnu --with-system-zlib * --program-suffix=-3.4.3 * Thread model: posix * gcc version 3.4.3 */
class BaseClass { protected: int V; public: virtual ~BaseClass() { } }; template<class T> class MiddleClass : public BaseClass { T Y; public: MiddleClass(int x, T y) : Y(y) { V = 1; /* OK */ } }; class DerivedClassThatWorks : public MiddleClass<float> { public: DerivedClassThatWorks(int x, float y) : MiddleClass<float>(x, y) { V = 5; /* OK */ } }; template<class T> class DerivedClassThatDoesNotWork : public MiddleClass<T> { public: DerivedClassThatDoesNotWork(int x, T y) : MiddleClass<T>(x, y) { V = 5; /* Compile Error */ } }; int main() { return 0; }