J'essaye d'écrire ces fonctions pour une affectation et je n'arrive pas à comprendre d'où ces erreurs proviennent ni ce qu'elles signifient. J'ai 2 classes, une pour le noeud et une pour l'arbre binaire. J'ai récemment ajouté certaines fonctions et leurs implémentations dans ma fonction principale, et elles causent des problèmes (principalement les fonctions publiques qui appellent les fonctions privées, je pense). Si les déclarations de fonctions semblent bizarres, je m'excuse, elles ont été écrites par mon instructeur, donc je ne peux pas vraiment les changer. Voici les fonctions avec lesquelles je travaille (je l'ai coupé ce qui est pas ou nouvelle):Erreur de compilation avec le constructeur de copie d'arbre binaire
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
}
est ici la poubelle incompréhensible mon compilateur me donne:
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]
Quelqu'un peut-il voir ce que J'ai mal fait dans ces quelques fonctions? Je dessine complètement un blanc.
EDIT :: Maintenant, je reçois une erreur de segmentation lorsque j'appelle mon constructeur de copie, mais il compile. À la demande, voici le code pour binTreeNode, mais c'est seulement pour la référence, je sais que cela fonctionne bien.
#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
};
nous montrer la définition de binTreeNode. – Henrik