2012-12-10 56 views
0

我無法弄清楚我的程序出了什麼問題。我已經使用頭文件中的類聲明和成員函數聲明,一個.cpp文件中的成員函數定義來構造它,然後我在main.cpp中有一個主驅動程序。我把它們都插入ideone一個文件,這樣我可以在這裏發佈的程序:帶有模板類的類實例化錯誤

http://ideone.com/PPZMuJ

上ideone顯示的錯誤確實存在錯誤時,建設我的IDE顯示。有人能指出我做錯了什麼嗎?

的錯誤是:

prog.cpp: In instantiation of ‘IntDLLNode<int>’: 
prog.cpp:56: instantiated from ‘void IntDLList<T>::addToDLLHead(const T&) [with T = int]’ 
prog.cpp:215: instantiated from here 
prog.cpp:8: error: template argument required for ‘struct IntDLList’ 

Line 56: head = new IntDLLNode<T>(el,head,NULL); 
Line 215: dll.addToDLLHead(numero); 
Line 8: class IntDLLNode { 

可以忽略的try/catch子句,我還沒有說完的那部分工作,但 - 我只是試圖讓過去的當前錯誤。

+0

u能顯示更多的代碼?這些是在不同領域的隨機線... neeed看到它的定義和一些代碼,一些上下文 –

回答

0

的問題是在朋友聲明:

template <class T> 
class IntDLLNode { 
    friend class IntDLList; 
    // rest of IntDLLNode here 
}; 

在這裏,您聲明一個非模板class IntDLList是朋友。稍後,您聲明一個模板具有相同名稱的類;但不像你期望它不會成爲IntDLLNode的朋友。

爲了解決這個問題,指定該友元類是模板:

template <class U> class IntDLList; 

template <class T> 
class IntDLLNode    { 
    friend class IntDLList<T>; 
    // rest of IntDLLNode here 
}; 
+0

謝謝!按預期工作。 – user1890265