2012-12-06 35 views
1

我想初始化非積分模板常量。C++非積分模板常量在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; 

我不能initializa恆聯,因爲它是一個非整數類型。

從我在線閱讀看來,如果我移動.cpp文件中的常量初始化,問題將消失。但是我不能這樣做,因爲我正在處理模板類。 我得到的錯誤,具體如下:

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

可有人請看看這對我,好嗎?非常感謝你的時間。

回答

1

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

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

謝謝你指出。我錯誤地補充說,雖然嘗試不同的解密變體來嘗試自己修復它。根據您的建議,我沒有編輯過我的問題。不過,編譯器仍然會出現同樣的錯誤。 – geraldCelente

+0

可能缺少'typename' then - 'typename ExtendedClass :: position'。 – Puppy

+0

你是男人。這固定了一切。謝謝! – geraldCelente