问题
I am trying to Initialize a Non-Integral template Constant.
Please find below the code:
#ifndef _EXETENDED_CLASS_H
#define _EXETENDED_CLASS_H
template<class T>
class BaseClass
{
public:
BaseClass();
~BaseClass();
};
template <class T>
BaseClass<T>::BaseClass()
{}
template <class T>
BaseClass<T>::~BaseClass()
{}
template<class T>
class ExtendedClass:public BaseClass<T>
{
public:
typedef ExtendedClass<T>* position;
static const position NULLPOSITION;
ExtendedClass();
~ExtendedClass();
private:
position _successivo;
};
template<class T>
const ExtendedClass<T>::position ExtendedClass<T>::NULLPOSITION = 0;
template <class T>
ExtendedClass<T>::ExtendedClass()
{}
template <class T>
ExtendedClass<T>::~ExtendedClass()
{}
#endif
The problem lies with the lines
template<class T>
const ExtendedClass<T>::position ExtendedClass<T>::NULLPOSITION = 0;
I cannot initializa the constant inline as it is of a Non-Integral Type.
From what i have read online it appears that if I moved the const initialization in a .cpp file, the issue would be gone. However I can't do that as I am dealing with a templated class. I get errors as detailed below:
ExtendedClass.h:43: error: expected init-declarator before "ExtendedClass"
ExtendedClass.h:43: error: expected `;' before "ExtendedClass"
make: *** [ExtendedClass.o] Error 1
Can someone please have a look at it for me, please? Thank you very much in advance for yout time.
回答1:
You've written the type twice, and didn't qualify the identifier. No wonder the poor compiler is confused.
template<class T>
const ExtendedClass<T>::position ExtendedClass<T>::NULLPOSITION = 0;
来源:https://stackoverflow.com/questions/13744823/c-non-integral-template-const-initialization-expected-init-declarator-before-c