Shoting in error

Please Check what is problem . it give error
Cerror: no matching function for call to 'TreeNode<char>::setInfo(char*&)'|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  #include <iostream>
#include <stdlib.h>
using namespace std;
template <class T>
class TreeNode{
private:
T *object;
TreeNode *left;
TreeNode *right;
public:
TreeNode(){
object = NULL;
left = NULL;
right = NULL;
}
TreeNode(T *object){
this->object = object;
left = NULL;
right = NULL;
}
void setInfo(T &object){
this->object = object;
}
void setLeft(TreeNode *left){
this->left = left;
}
void setRight(TreeNode *right){
this->right = right;
}
T getinfo(){
return object;
}
TreeNode * getLeft(){
return this->left;
}
TreeNode* getRight(){
return right;
}
int isleaf(){
if (left == NULL &&right == NULL)
return 1;
else
return 0;
}
void inorder(TreeNode <char>* tree){
if (tree != NULL){
inorder(tree->getLeft());
cout << tree->getinfo();
inorder(tree->getRight());
}
}
void insert(TreeNode <char>* root, char *info){
TreeNode <char> node = new TreeNode <char>(info);
TreeNode *p, *q;
p = q = root;
while (strcmp(info, p.getinfo()) != 0 && q == NULL){
p = q;
if (strcmp(info, p.getinfo()) < 0)
q = p.getLeft();
else
q = p.getRight();
}
if (strcmp(info, p.getinfo()) == 0){
cout << "Dupilcate :" << *info;
delete node;
}
if (strcmp(info, p.getinfo()) > 0)
p.setright(node);
else
p.setLeft(node);
}
};
int main(){
TreeNode <char> *root = new TreeNode <char>();
static char * w[] = { "apple", "bat", "zat", "gain", "car", NULL };
root->setInfo(w[0]);
for (int i = 0; w[i]; i++){
insert(root, w[i]);
}
inorder(root);
cout << end;
}
You defined your TreeNode instance as a <char> type, yet you try to call setInfo() with a char*.

Why the dynamic memory? Why not just use a "normal" instance of your class instead of the pointer (*root)?

TreeNode<char*> root;

You really really need to find an indentation style you like and use it consistently. Your program, as presented, is very hard to read.

Lastly why the C-strings? Using C++ strings would make this program a lot easier. There are several other possible problems being caused by using the C-strings instead of C++ strings. And why the template class? C-strings will require many specialized functions, because you can't use the comparison, or assignment operators with C-strings so a non-template version would probably be better.



Topic archived. No new replies allowed.