C++ Non-Integral template Const Initialization expected init-declarator before ClassName

会有一股神秘感。 提交于 2019-12-23 02:26:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!