Jerome Durand wrote:
Hello,
I'm trying to write something along the following lines
but I cannot get this to compile.
template <typename derived> struct Base {
typedef typename derived::valueType valueType;
virtual valueType Value() = 0;
};
struct CharValue: Base<CharValue>{
typedef char valueType ;
valueType Value() {return 'a';}
};
struct IntValue: Base<IntValue> {
typedef int valueType ;
valueType Value() {return 1234;}
};
The compiler outputs (first error only):
Error 1: 'valueType' : is not a member of CharValue'
Could someone tell me what is wrong with this?
It can be that the compiler is trying to resolve 'derived::valueType'
too soon (at the time when it encounters the 'typedef' in 'Base', the
'derived' is not completely defined yet. The behaviour of compilers
in this matter can differ somewhat, but the most concervative will
likely choke because 'derived' is incomplete by the time 'Base'
instantiation is attemtped. You might consider replacing the 'typedef'
dance with normal type processing:
template<typename valueType> struct Base {
virtual valueType Value() = 0;
};
struct CharValue : Base<char> {
char Value() { return 'a'; }
};
...
V
types which should depend on types defined in derived...