我想寫一個任務的這些函數,我無法弄清楚這些錯誤來自哪裏或他們的意思。我有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
};
顯示我們binTreeNode的定義。 – Henrik 2012-03-27 06:40:34