Problem with derived templates
Ballerstädt, Axel
ABA@EMH.DE
Tue Mar 23 10:40:00 GMT 2010
Hello to everyone,
I have a problem with a derived template class. It seams that all the members of the base class are not visible in the derived class. If I then make an instance of the derived class, I can access all members of the derived and the base class ??
I'm using the CodeSourcery gcc4.4.1 (tried also 4.3.3, 4.3.2) to compile the following.
Im sorry, my english is not very good, so please have a look at the sourecode:
template <int N> class TBase {
public:
int Array[N];
int fb1() { return Array[0]; };
}; // everything is OK in TBase
template <int NN> class TDerived : public TBase<NN> {
int UU;
public:
int fc2() { return Array[0]; }; // error: 'Array' was not declared in this scope
int fc3() { return UU; }; // OK
int fc4() { return fb1(); }; // error: .... 'fb1' must be available
};
int main() {
TDerived<10> Derived;
Derived.Array[2] = 5; // OK
Derived.UU = 1; // correct error, because UU is private
Derived.fb1(); // OK
return 0;
}
Why is it not possible to access the Members of the base template class TBase in the scope of the derived class TDerived ?
I can fix this by writing:
int fc2() { return TBase<NN>::Array[0]; };
but in our production code, the base class is a template with 4 parameters and its members are used a lots of times in the derived classes, so changing all these does not make the code very readable.
Isnt there a compiler switch that affect this behavior ?
The same code compiles in microsoft visual studio V6, borland CBuilder6 and my old microtec embedded compiler, so whats wrong with gcc.
mit freundlichen Grüssen / best regards,
--------------------------------------------------------------------------------------------
EMH Energie-Messtechnik GmbH
Axel Ballerstädt
Tel.: +49 (4185) 5857 10
Fax.: +49 (4185) 5857 68
Vor dem Hassel 2 email: ballerstaedt@emh.de
D-21438 Brackel web: http://www.emh.eu
Eingetragen beim Amtsgericht Lüneburg HRB 110177
Geschäftsführer: Alfred Meyer, Dipl.-Ing. Karsten Schröder
--------------------------------------------------------------------------------------------
More information about the Gcc-help
mailing list