2012-05-13 28 views
0

g ++編譯器提示下列說法錯誤的是:模板類的實例化是錯誤的

template<typename Type> 
class SingleList{ 
public: 
    SingleList() 
     { 
      head = new SingleListNode<Type>() ; //Error happens here!!! 
     } 

的錯誤信息是:

./inc/single_list.h: In instantiation of ‘SingleListNode<int>’: 
./inc/single_list.h|39 col 13| instantiated from ‘SingleList<Type>::SingleList() [with Type = int]’ 

head定義如下,也許問題這裏沒有關係。

SingleListNode<Type> *head; 

SingleList類的main功能的實例是:

int main() 
{ 
    SingleList<int> list; 

我不知道在哪裏語法錯誤發生,誰能幫助我?謝謝!!

============================================== ==========================

以下是源文件的內容:

template<typename Type> class SingleList; 

template<typename Type> class SingleListNode{ 
private: 
    friend class SingleList<Type>; 

    SingleListNode() : next(NULL){} 

public: 
    friend ostream& operator<< <Type>(ostream& os,SingleListNode<Type>& sln);    //Error here!! 

private: 
    SingleListNode *next; 
}; 

template<typename Type> ostream& operator<<(ostream& os,SingleListNode<Type>& out){ 
    os<<out.data; 
    return os; 
} 

template<typename Type> class SingleList{ 
public: 
    SingleList() 
     { 
      head = new SingleListNode<Type>() ;    //Error happens here. 
     } 
    ~SingleList(){ 
     delete head;          //Same error 
    } 

private: 
    SingleListNode<Type> *head; 
}; 

錯誤消息提示用g ++

|| g++ -g -I ./inc/ -c single_list_test.cpp -o single_list_test.o 
|| single_list.h: In instantiation of ‘SingleListNode<int>’: 
single_list.h|25 col 13| instantiated from ‘SingleList<Type>::SingleList() [with Type = int]’ 
single_list_test.cpp|9 col 18| instantiated from here 
single_list.h|10 col 18| error: template-id ‘operator<< <int>’ for ‘std::ostream& operator<<(std::ostream&, SingleListNode<int>&)’ does not match any template declaration 
|| make: *** [single_list_test.o] Error 1 
+8

這不是錯誤消息,它只是告訴我們*,其中*錯誤的是(上下文),而不是* *什麼是。此外,如果您發佈*完整*,*最小*代碼來展示問題,這將有所幫助。像這樣的高度不完整的代碼片段比他們幫助我們更晦澀難懂。 –

+0

謝謝,我添加了完整的代碼。 – river

+0

你錯過了我說的發佈「最小」代碼。這個代碼並不是最小的,它包含了很多與問題無關的東西。您還沒有發佈錯誤消息,並且我懷疑你發佈了錯誤的代碼,因爲錯誤不會在你說的時候發生。 –

回答

1

爲了我的gcc,只需修改下面的語句

friend ostream& operator<< <Type>(ostream& os,SingleListNode<Type>& sln) 

friend ostream& operator<< (ostream& os,SingleListNode<Type>& sln)