簡體   English   中英

C ++非整體模板Const初始化,需要在ClassName之前使用init-declarator

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

我正在嘗試初始化非整數模板常量。

請在下面的代碼中找到:

#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

問題出在線條上

template<class T>
const  ExtendedClass<T>::position ExtendedClass<T>::NULLPOSITION = 0;

我不能初始化常量內聯,因為它是非整數類型。

從我在線閱讀的內容來看,如果將const初始化移動到.cpp文件中,則問題將消失。 但是,由於要處理模板化類,所以無法做到這一點。 我收到如下錯誤的詳細信息:

ExtendedClass.h:43: error: expected init-declarator before "ExtendedClass"
ExtendedClass.h:43: error: expected `;' before "ExtendedClass"
make: *** [ExtendedClass.o] Error 1

有人可以幫我看看嗎? 非常感謝您抽出寶貴的時間。

您已經寫了兩次該類型,並且沒有限定標識符。 難怪可憐的編譯器很困惑。

template<class T>
const  ExtendedClass<T>::position ExtendedClass<T>::NULLPOSITION = 0;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM