2013-10-13 142 views
1

我聲明瞭一個模板類threadBinaryTree和功能NULL與模板參數不匹配?

void threadBinaryTree<T>::inThread 
    (threadBinaryTreeNode<T>*root,threadBinaryTreeNode<T>*& pre) 

但符合錯誤:

no matching function for call to ‘threadBinaryTree<char>::inThread 
     (threadBinaryTreeNode<char>*, NULL)’| 

pre需要被初始化爲NULL,我應該怎麼辦?

+1

東西你試過''nullptr''?就我所知,「NULL」不是C++。 –

+0

從代碼中的簽名開始,pre必須引用現有的指針。 – Ashalynd

+2

@Jonas'NULL'在C++中,因爲它在C stdlib中。儘管由於更好的重載解析行爲,'nullptr'是首選。 – rubenvb

回答

4

你的第二個參數需要一個非const左值引用某種指針,但是你傳遞一個右值(NULL)。您不能將右值綁定到非常量左值引用。你需要通過一個左:

threadBinaryTreeNode<T>* p = NULL; 
x.inThread(somePtr, p); 
1

第二個參數是threadBinaryTreeNode<T>*& pre,所以你不能傳遞NULL它。

threadBinaryTreeNode<T> *empty = 0; // Pass empty to the method instead of NULL 

此外,最好使用0nullptr而非NULL

0

因爲你的第二個參數的功能,你需要提供一個可變的非const引用,這樣

threadBinaryTreeNode<char>* ptr = NULL; 
inThread(..., ptr);