Returning a pointer to an object

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<iostream>
using namespace std;

class btree
{
      struct node
      {
             node *left;
             int data;
             node *right;
      };
      
      public:
             node *root;
             btree();
             void insert(node*,int);
             void inorder(node*);
             /*void postorder(node*);
             void preorder(node*);
             void del(node*);*/
             node* find(node*,int);
};

btree::btree()
{
             root=NULL;
}

node* btree::find (node *temp, int item)
{
      if(temp==NULL)
      return temp;
      else if(temp->data==item)
      return temp;
      else if(temp->data<item)
      {
           if(temp->right==NULL)
           return temp;
           else return find(temp->right,item);
      }
      else 
      {
           if(temp->left==NULL)
           return temp
           else return find(temp->left,item);
      }
}
      
void btree:: insert(node *temp, int item)
{
     node *place=find(temp,item);
     
     if(place==NULL)
     {
                   temp=new node;
                   temp->data=item;
                   temp->right=NULL;
                   temp->left=NULL;
                   root=temp;
     }
     
     if(item<place->data)
     {
                     place->left=new node;
                     place->left->data=item;
                     place->left->left=NULL;
                     place->left->right=NULL;
     }   
     
     if(item>place->data)
     {
                     place->right=new node;
                     place->right->data=item;
                     place->right->left=NULL;
                     place->right->right=NULL;
     }
}                               

void btree::inorder(node *temp)
{
     if(temp==NULL)
     return;
     else
     {
         inorder(temp->left);
         cout<<temp->data<<" ";
         inorder(temp->right);
     }
}                   
     
int main()
{
    btree a;
    for(int i=1;i<100;i++)
    a.insert(a.root,i);
    
    a.inorder(a.root);
    system("pause");
    return 0;
}
                   

29 C:\Dev-Cpp\BST.cpp expected constructor, destructor, or type conversion before "btree" 
29 C:\Dev-Cpp\BST.cpp expected `,' or `;' before "btree" 


// Why am I getting this error? Where am I going wrong?
Last edited on
My question is not showing up on the forum. :( Someone help please..
The return type on line 29 should be btree::node. You are missing a semicolon on line 44.
Returning a pointer to an object
Remove #include<iostream> using namespace std; class btree { struct node { ...
Sep 9, 2012 at 4:34pm [2 replies] Last: The return type on line 29 should be btree::node . You are missing a ... (by Peter87)

Hey Peter,
In the My Topics page, all I can see is that^, but when I open this page, I can see only my comment and question. What is the problem? Please PM me. I'm new on this site.
Thanks
oops! now found it.. had to refresh the page after opening it once.. weird!
nope.. it is still not working.. if I do modify line 29 to be btree::node, that would mean that the function would return an integer, whereas I am returning a pointer to a node.
Sorry, I mean btree::node*.
That is not working as well .. Too many errors.. Can you please compile the program once and check?
Last edited on
When I do the two changes the program compiles for me. It crash if I try to run it though.
Last edited on
node* find(node*,int); btree::node* find (node *temp, int item)

Is this the final thing? I have made the other ";" entry. Program still does not compile.
What error message do you get?
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<iostream>
using namespace std;

class btree
{
      
      struct node
      {
             node *left;
             int data;
             node *right;
      };
      
      public:
             node *root;
             btree();
             void insert(node*,int);
             void inorder(node*);
             /*void postorder(node*);
             void preorder(node*);
             void del(node*);*/
             node* find(node*,int);
};

btree::btree()
{
             root=NULL;
}

btree::node* find (node *temp, int item)
{
      if(temp==NULL)
      return temp;
      else if(temp->data==item)
      return temp;
      else if(temp->data<item)
      {
           if(temp->right==NULL)
           return temp;
           else return find(temp->right,item);
      }
      else 
      {
           if(temp->left==NULL)
           return temp;
           else return find(temp->left,item);
      }
}
      
void btree:: insert(node *temp, int item)
{
     node *place=find(temp,item);
     
     if(place==NULL)
     {
                   temp=new node;
                   temp->data=item;
                   temp->right=NULL;
                   temp->left=NULL;
                   root=temp;
     }
     
     if(item<place->data)
     {
                     place->left=new node;
                     place->left->data=item;
                     place->left->left=NULL;
                     place->left->right=NULL;
     }   
     
     if(item>place->data)
     {
                     place->right=new node;
                     place->right->data=item;
                     place->right->left=NULL;
                     place->right->right=NULL;
     }
}                               

void btree::inorder(node *temp)
{
     if(temp==NULL)
     return;
     else
     {
         inorder(temp->left);
         cout<<temp->data<<" ";
         inorder(temp->right);
     }
}                   
     
int main()
{
    btree a;
    for(int i=1;i<100;i++)
    a.insert(a.root,i);
    
    a.inorder(a.root);
    system("pause");
    return 0;
}
                   


30 C:\Dev-Cpp\BST.cpp `node' was not declared in this scope 
30 C:\Dev-Cpp\BST.cpp `temp' was not declared in this scope 
30 C:\Dev-Cpp\BST.cpp expected primary-expression before "int" 
31 C:\Dev-Cpp\BST.cpp initializer expression list treated as compound expression 
31 C:\Dev-Cpp\BST.cpp expected `,' or `;' before '{' token 
You should not remove btree:: in front of find.
btree::node* btree::find(node *temp, int item)
Last edited on
Hey.. Why do we do that? I do not get the reason behind it.. The program does compile now but there is no output..
Rishav Paul wrote:
Why do we do that? I do not get the reason behind it..

The first btree:: is needed because node is defined inside btree. Without btree:: it thinks we mean node in the global namespace, which is not defined so you get an error message.

The second btree:: is needed because we want to define a member function of btree, not a non-member function in the global namespace.

Rishav Paul wrote:
The program does compile now but there is no output..

The program crashes because you try to use a null pointer on line 63.
Last edited on
wow! I did get the reason why we use the scope operator twice. Thank you so much.

On line 63, I have if(item<place->data).. I should use else if I suppose..
I got the output! :) "Happiest man on Earth " now. Thank you so much. Lot of concepts got cleared today. But I will be needing more help I'm sure
Sir, how is the logic behind my program? Is it too basic? Can it be done in a more elegant and short manner using lesser memory/time?
I guess this is a school assignment so don't worry too much about memory/time efficiency. Elegant and easy to read code is much more important. It's probably possible write the find function a bit shorter but it's no big deal in my opinion.

The indentation doesn't look perfect (maybe it's just an issue when copying it to this site?).
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include<iostream>
using namespace std;

class btree
{
      public:
      struct node
      {
             node *left;
             int data;
             node *right;
      };
            
             node *root;
             btree();
             void insert(node*,int);
             void inorder(node*);
             void postorder(node*);
             void preorder(node*);
             void del(node*);
             node* find(node*,int);
};

btree::btree()
{
             root=NULL;
}

btree::node* btree::find(node *temp, int item)
{
      if(temp==NULL)
             return temp;
             
      else if(temp->data<item)
      {
             if(temp->right!=NULL)
             return find(temp->right,item);
      }
      else 
      {
             if(temp->left!=NULL)
             return find(temp->left,item);
      }
      return temp;
}
      
void btree:: insert(node *temp, int item)
{
     node *place=find(temp,item);
     
     if(place==NULL)
     {
             temp=new node;
             temp->data=item;
             temp->right=NULL;
             temp->left=NULL;
             root=temp;
     }
     
     else if(item<place->data)
     {
             place->left=new node;
             place->left->data=item;
             place->left->left=NULL;
             place->left->right=NULL;
     }   
     
     else if(item>place->data)
     {
             place->right=new node;
             place->right->data=item;
             place->right->left=NULL;
             place->right->right=NULL;
     }
}                               

void btree::inorder(node *temp)
{
     if(temp==NULL)
            return;
     else
     {
            inorder(temp->left);
            cout<<temp->data<<" ";
            inorder(temp->right);
     }
}

void btree::preorder(node *temp)
{
     if(temp==NULL)
            return;
     else
     {
            cout<<temp->data<<" ";
            preorder(temp->left);
            preorder(temp->right);
     }
}

void btree::postorder(node *temp)
{
     if(temp==NULL)
            return;
     else
     {
            postorder(temp->left);
            postorder(temp->right);
            cout<<temp->data<<" ";
     }
}                         
     
int main()
{
    btree a;
    
    a.insert(a.root,9);
    a.insert(a.root,3);
    a.insert(a.root,6);
    a.insert(a.root,1);
    a.insert(a.root,8);
    a.insert(a.root,2);
    a.insert(a.root,5);
    a.insert(a.root,7);
    a.insert(a.root,4);
    a.insert(a.root,10);
    
    a.inorder(a.root);
    cout<<endl;
    a.preorder(a.root);
    cout<<endl;
    a.postorder(a.root);
    cout<<endl;
    system("pause");
    return 0;
}


Is the indentation proper now? I never really learnt this thing.
I have modified the find() function. Is that what you were talking about?
Topic archived. No new replies allowed.