2012-03-27 89 views
0

我想寫一個任務的這些函數,我無法弄清楚這些錯誤來自哪裏或他們的意思。我有2個類,一個用於節點,另一個用於二叉樹。我最近在主函數中添加了一些函數和它們的實現,並且它們引發了問題(主要是我認爲稱爲私有函數的公共函數)。如果函數聲明看起來很奇怪,我很抱歉,它們是由我的教師編寫的,所以我不能真正改變它們。這裏是我正在使用的功能(我剪了什麼是不相關的或新的):編譯器錯誤與二進制樹複製構造函數

template < class T > class binTree { 
public: 
binTree () { // default constructor 
    root = NULL; 
} 
binTree (const binTree<T>& Right) { // Copy Constructor 
    root = copy(Right.root); 
} 
virtual ~binTree() { // Destructor 
    clear(root); 
} 
binTree<T>& operator = (const binTree<T>& Right) { // assignment operator 
    if (root != NULL) 
     clear(root); 
    root = copy(Right.root); 
    return *this; 
} 
void clear() { 
    clear(root); 
} 
protected: 
binTreeNode <T>* root; // root of tree 
private: 
void clear (binTreeNode <T>*& p) { 
    if (p != NULL) { 
     clear(p->left); 
     clear(p->right); 
     delete p; 
     p = NULL; 
    } 
} 
binTreeNode<T>* copy(const binTreeNode<T>* p) { 
    if (p != NULL) { 
     binTreeNode<T>* newNode; 
     newNode = new binTreeNode<T>(*p); // modified 
     newNode->left = copy(p->left); 
     newNode->right = copy(p->right); 
     return newNode; 
    } 
     return NULL; // Added 
} 

這裏是難以理解的垃圾我的編譯器給我:

In file included from prog7.cc:2:0: 
binTree.h: In member function ‘binTreeNode<T>* binTree<T>::copy(const binTreeNode<T>*) [with T = int]’: 
binTree.h:16:3: instantiated from ‘binTree<T>::binTree(const binTree<T>&) [with T = int]’ 
prog7.cc:13:36: instantiated from here 
binTree.h:96:4: error: invalid conversion from ‘const binTreeNode<int>*’ to ‘int’ [-fpermissive] 
binTreeNode.h:12:2: error: initializing argument 1 of ‘binTreeNode<T>::binTreeNode(const T&, binTreeNode<T>*, binTreeNode<T>*) [with T = int]’ [-fpermissive] 
binTree.h: In member function ‘binTreeNode<T>* binTree<T>::copy(const binTreeNode<T>*) [with T = float]’: 
binTree.h:24:3: instantiated from ‘binTree<T>& binTree<T>::operator=(const binTree<T>&) [with T = float]’ 
prog7.cc:29:14: instantiated from here 
binTree.h:96:4: error: no matching function for call to ‘binTreeNode<float>::binTreeNode(const binTreeNode<float>*&)’ 
binTree.h:96:4: note: candidates are: 
binTreeNode.h:12:2: note: binTreeNode<T>::binTreeNode(const T&, binTreeNode<T>*, binTreeNode<T>*) [with T = float] 
binTreeNode.h:12:2: note: no known conversion for argument 1 from ‘const binTreeNode<float>*’ to ‘const float&’ 
binTreeNode.h:8:28: note: binTreeNode<float>::binTreeNode(const binTreeNode<float>&) 
binTreeNode.h:8:28: note: no known conversion for argument 1 from ‘const binTreeNode<float>*’ to ‘const binTreeNode<float>&’ 
binTree.h: In member function ‘binTreeNode<T>* binTree<T>::copy(const binTreeNode<T>*) [with T = std::basic_string<char>]’: 
binTree.h:16:3: instantiated from ‘binTree<T>::binTree(const binTree<T>&) [with T = std::basic_string<char>]’ 
prog7.cc:39:30: instantiated from here 
binTree.h:96:4: error: no matching function for call to ‘binTreeNode<std::basic_string<char> >::binTreeNode(const binTreeNode<std::basic_string<char> >*&)’ 
binTree.h:96:4: note: candidates are: 
binTreeNode.h:12:2: note: binTreeNode<T>::binTreeNode(const T&, binTreeNode<T>*, binTreeNode<T>*) [with T = std::basic_string<char>] 
binTreeNode.h:12:2: note: no known conversion for argument 1 from ‘const binTreeNode<std::basic_string<char> >*’ to ‘const std::basic_string<char>&’ 
binTreeNode.h:8:28: note: binTreeNode<std::basic_string<char> >::binTreeNode(const binTreeNode<std::basic_string<char> >&) 
binTreeNode.h:8:28: note: no known conversion for argument 1 from ‘const binTreeNode<std::basic_string<char> >*’ to ‘const binTreeNode<std::basic_string<char> >&’ 
binTree.h: In member function ‘binTreeNode<T>* binTree<T>::copy(const binTreeNode<T>*) [with T = int]’: 
binTree.h:101:2: warning: control reaches end of non-void function [-Wreturn-type] 
binTree.h: In member function ‘binTreeNode<T>* binTree<T>::copy(const binTreeNode<T>*) [with T = float]’: 
binTree.h:101:2: warning: control reaches end of non-void function [-Wreturn-type] 
binTree.h: In member function ‘binTreeNode<T>* binTree<T>::copy(const binTreeNode<T>*) [with T = std::basic_string<char>]’: 
binTree.h:101:2: warning: control reaches end of non-void function [-Wreturn-type] 

任何人都可以看到什麼我在這幾個功能上做錯了?我完全畫空白。

編輯::現在我收到一個分段錯誤,當我打電話給我的複製構造函數,但它確實編譯。通過請求,這裏是binTreeNode的代碼,但它僅供參考,我知道它工作正常。

#pragma once 
template < class T > class binTree; 
template < class T > class binTreeNode { 
friend class binTree <T>; 
public: 
    // default constructor 
    binTreeNode (const T& newData =T(), binTreeNode <T>* newLeft = 0, binTreeNode <T>* newRight = 0) { 
     data = newData; 
     left = newLeft; 
     right = newRight; 
    } 
private: 
    T data; // data value in node 
    binTreeNode <T> *left, *right; // links to other nodes 
}; 
+0

顯示我們binTreeNode的定義。 – Henrik 2012-03-27 06:40:34

回答

0

有至少一個錯誤的位置:

binTreeNode<T>* copy(const binTreeNode<T>* p) { 
    if (p != NULL) { 
     binTreeNode<T>* newNode; 
     newNode = new binTreeNode<T>(p); 

如果你想使p深層副本,則需要取消對它的引用的拷貝構造函數:

 newNode = new binTreeNode<T>(*p); 
+0

這解決了編譯錯誤,謝謝!現在我調用我的拷貝構造函數時出現了分段錯誤。 – Nathan 2012-03-27 14:21:48

0

我猜你需要的情況下,p == NULL的複製方法返回的東西:

binTreeNode<T>* newNode = NULL; 
if (p != NULL) { 
    newNode = new binTreeNode<T>(p); 
    newNode->left = copy(p->left); 
    newNode->right = copy(p->right); 
} 
return newNode; 

而在C++中是很常見使用0而不是NULL

+0

我想這取決於誰教你。我總是被教導在C++中使用NULL。 – trutheality 2012-03-27 05:16:10

+0

好趕,我加了一個返回NULL;但那並沒有真正擺脫任何錯誤。 – Nathan 2012-03-27 05:33:03

+0

@trutheality可能你是對的,斯里。 – 2012-03-27 06:12:04

0

錯誤總是指binTreeNode。爲什麼不告訴我們關於binTreeNode定義的代碼?