2011-11-28 144 views
1

我花了幾個小時在網上搜索解決方案,但無濟於事。我在Xcode中編程C++C++模板類專業化和結構

#import "data.h" // contains a struct called data 

template <class T> 
class container { 
    public: 
     container(); 
     ~container(); 
    private: 
     // functionality for containing T 
}; 

template <class T> 
container<T>::container() { /* generic */ } 

template <class T> 
container<T>::~container() { /* generic */ } 

template <> 
container<data>::container() { /* template specialization of data */ } 

編譯器抱怨:重複符號並指出類模板特化。我想,也許是因爲結構不能夠專注的,所以我想沿增加一個額外的void函數

template <class T> 
class container { 
    public: 
     container(); 
     ~container(); 
     void setup(); 
    private: 
     // functionality for containing T 
}; 

template <> 
void container<data>::setup() { /* template specialization of data */ } 

的線條的東西,但是這給了我同樣的編譯器錯誤。我真的不知道現在在哪裏尋找解決方案...

+0

這是什麼樣的模板專業化?寫另一個專業類。 –

回答

1

當您專門化一個類模板時,必須專門化所有成員函數。

除了設置,您仍然需要專門構造/析構函數。

template <> 
container<data>::container() 
{ 
    // ... 
} 

template <> 
container<data>::~container() 
{ 
    // ... 
} 
+0

謝謝,這是它。雖然我的模板類有很多不需要專業化的功能。我想我會做一個單獨的包裝類。 – rwols