Re: Undefined class when class is already defined
On 2011-02-11 00:00, Homero C. de Almeida wrote:
Hi everybody,
I have a code structured somewhat as follows:
class BaseRef {
protected :
void* ptr;
};
template<class T>
TempRef : public BaseRef {
// This class keeps a reference to a T instance
// Using the ptr defined in BaseRef
};
You are missing here the class or struct keyword in front of TempRef.
class Base {
};
class A: public Base { };
template<class T> Base2: Base {
You are missing here the class or struct keyword in front of Base.
public:
inline TempRef<AnotherClass> method1( ) const { return
YetAnotherClass::staticMethod(); }
};
Note that YetAnotherClass is a non-dependent type and
YetAnotherClass::staticMethod() is a non-dependent construct, therefore
the compiler is supposed to evaluate it immediately - but at this point
YetAnotherClass is neither declared *nor* a complete type.
Also, AnotherClass is not even declared and needs at least a forward
declaration.
You need to move the implementation of TempRef<AnotherClass> method1()
const to a later point.
class SomeOtherClass: Base2<A> {};
class AnotherClass : Base {
public:
SomeOtherClass method2( );
};
class YetAnotherClass {
public:
static TempRef<AnotherClass> staticMethod( );
};
When i try to compile it, the compiler complains that method1 uses an
undefined class "TempRef<AnotherClass>". However, the headers with the
template and the class definition for TempRef and AnotherClass are
included in the code.
Yes, they are available, but the definition is too late, because of the
non-dependent context.
You should refactor the code, e.g. to something like
class BaseRef {
protected :
void* ptr;
};
template <class T>
class TempRef : public BaseRef {};
class Base {};
class A: public Base {};
class AnotherClass;
template <class T>
class Base2 : Base {
public:
inline TempRef<AnotherClass> method1( ) const;
};
class SomeOtherClass : Base2<A> {};
class AnotherClass : Base {
public:
SomeOtherClass method2();
};
class YetAnotherClass {
public:
static TempRef<AnotherClass> staticMethod();
};
template <class T>
inline TempRef<AnotherClass>
Base2<T>::method1( ) const {
return YetAnotherClass::staticMethod();
}
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]