我正在尝试为作业编写这些函数,但我无法弄清楚这些错误的来源或含义。我有2个类,一个用于节点,一个用于二叉树。我最近在我的main函数中添加了一些函数及其实现,并且它们引起了问题(我认为主要是调用私有函数的公共函数)。如果函数声明看起来很奇怪,我道歉,它们是由我的教师编写的,所以我无法真正改变它们。这是我正在使用的功能(我已经删除了不相关或新的功能):
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 :(得分: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
。
答案 1 :(得分:0)
错误总是引用binTreeNode。为什么不向我们展示有关binTreeNode定义的代码?
答案 2 :(得分: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);