cannot convert from Node<T>* to const int

Pages: 12
I am working on homework using a binary search tree. My professor provided us with files to modify and add two methods to one of the files. The 2 new methods are called void find_node( const T &val ) and void delete_node( Node< T > *&p ). I have some code written out, but when I try to compile it, I receive error messages saying that cannot convert parameter 1 from Node<T>* to const int* Wondering if anyone could help me with this.

btree.h
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
#include    <iostream>
#include    "node.h"
//using namespace std;

template < typename elemType >
class BinaryTree {
  public:
    BinaryTree( );
    ~BinaryTree( );
    void insert( const elemType & );
    void remove( const elemType & );
    void inorder( );
    bool empty( );
    void clear( );

  private:
    Node< elemType >    *_root;

    BinaryTree( const BinaryTree & );
    BinaryTree &operator =( const BinaryTree & );
    void clear( Node< elemType > * );
};

template < typename elemType >
inline BinaryTree< elemType >::BinaryTree( ) : _root(0)
{
    cout << "BinaryTree::BinaryTree() "
         << "default constructor\n";
}

template < typename elemType >
inline BinaryTree< elemType >::~BinaryTree( )
{
    cout << "BinaryTree::~BinaryTree() destructor\n";
    clear( );
}

template < typename elemType >
inline void BinaryTree< elemType >::clear( )
{
    if( _root )
    {
        clear( _root );
        _root = 0;
    }
}

template < typename elemType >
inline void BinaryTree< elemType >::clear( Node< elemType > *pt )
{
    if( pt ) {
        cout << "clear( ) left of  "  << pt->value( ) << '\n';
        clear( pt->left( ) );
        cout << "clear( ) right of " << pt->value( ) << '\n';
        clear( pt->right( ) );
        delete pt;
    }
}

template < typename elemType >
inline void BinaryTree< elemType >::insert( const elemType &e )
{
    if( !_root )
    {
        _root = new Node< elemType >( e );
    }
    else
    {
        _root->insert_value( e );
    }
}

template < typename elemType >
inline void BinaryTree< elemType >::remove( const elemType &e )
{
    _root->find_node( _root, e);
}

template < typename elemType>
inline void BinaryTree< elemType >::inorder( )
{
    _root->inorder( _root );
    cout << '\n';
}


node.h
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233

#ifndef     NODE_H

#define     NODE_H

#include    <string>

using namespace std;

template< typename T >
class Node
{
    public:
        Node( const T &);

        T  value( )  const;
        T  value( const T & );

        void insert_value( const T & );
        void inorder( const Node * );
		void find_node( Node<T> *root, const T &val)const;
		//bool find_node( const T &val, Node < T > *node) const;
		void delete_node( Node< T > *&p );

        Node *  left ( ) const;
        Node *  left ( Node * );
        Node *  right( ) const;
        Node *  right( Node * );
		
    private:
        T       _value;
        Node *  _left;
        Node *  _right;
		Node * root;	//point to root node
        Node::Node( const Node & );
        Node &operator =( const Node & );
		

	
};


template< typename T >
inline Node< T >::Node( const T &rhs )
{
    _value = rhs;                       // assign rhs to _value
    _left  =  _right = 0;               // node is not part of a tree yet
}


template< typename T >
inline T Node< T >::value( ) const
{
    return _value;
}


template< typename T >
inline T Node< T >::value( const T &rhs )
{
    _value = rhs;                       // new value for _value
    return _value;
}


template< typename T >
inline Node< T > *Node< T >::left( ) const
{
    return _left;
}


template< typename T >
inline Node< T > *Node< T >::left( Node< T > *rhs )
{
    _left = rhs;

	return _left;
}


template< typename T >
inline Node< T > *Node< T >::right( ) const
{
    return _right;
}


template< typename T >
inline Node< T > *Node< T >::right( Node< T > *rhs )
{
    _right = rhs;
    return _right;
}


template< typename T >
inline void Node< T >::insert_value( const T &val )
{
    if( val == _value )
    {
        return;                     // value already in the tree
    }
    if( val < _value )              // val should appear at the left
    {
        if( ! _left )               // no left subtree ?
        {                           // add new node here
            _left = new Node( val );
        }
        else                        // try the subtree
        {
            _left->insert_value( val );
        }
    }
    else                            // val should appear at the right
    {
        if( ! _right )              // no right subtree ?
        {                           // add new node here
            _right = new Node( val );
        }
        else                        // try the subtree
        {
            _right->insert_value( val );
        }
    }
}


template< typename T >
inline void Node< T >::inorder( const Node< T > *pt )
{
    if( pt )
    {
        inorder( pt->_left );
        cout << std::hex << pt->_left << std::dec << '\t';
        cout << std::hex << pt << std::dec << '\t';
        cout << std::hex << pt->_right << std::dec << '\t';
        cout << pt->_value << '\n';
        inorder( pt->_right );
    }
}



template <typename T>
const T * find_node(  Node<T> *root, const T &val) 
{
     const T * curr = root;
    while( curr != 0 )
    {
        if (val == curr -> _value)  {
           break;
        }
        else if (val < curr -> _value) {
            curr = curr -> _left;
        |
        else {
            curr = curr -> _right;
        }
    }
    return curr;
    }
}

template <typename T >
inline void Node < T> :: find_node(  Node< T > *root, const T &val) const 
{
    const T & ptrFoundNode = find_node( val, root );
    if( ptrFoundNode ) 
	{
        delete_node( ptrFoundNode, root );
    }
}


template< typename T >
inline void Node< T >::delete_node(Node < T > *&p)
{

	Node<T> *curr, *prev, *temp;
    if (p == NULL) return;
    if (p->_left == NULL && p->_right == NULL) {
        // no children - easy
        // *** if allowing counted duplicates:
        // ***    if (p->getCount() > 1) (*p)--;
        // ***    else {
           temp = p;
           p = NULL;
           delete temp;
        }
     else if (p->_left == NULL) {
        // only a right child - still easy
        // *** if allowing counted duplicates:
        // ***    if (p->getCount() > 1) (*p)--;
        // ***    else {
           temp = p;
           p = temp->_right;
           delete temp;
        }
     else if (p->_right == NULL) {
        // only a left child - still easy
        // *** if allowing counted duplicates:
        // ***    if (p->getCount() > 1) (*p)--;
        // ***    else {
           temp = p;
           p = temp->_left;
           delete temp;
        }
     else {
        // two children - this is the hard case
        // use successor: once right, then as far left as possible
        // *** if allowing counted duplicates:
        // ***    if (p->getCount() > 1) (*p)--;
        // ***    else {
           curr = p->_right;
           prev = NULL;
           while (curr->_left != NULL) {
               prev = curr;
               curr = curr->left;
           }
           p->_value = curr->_value;
           if (prev == NULL) p->_right = curr->_right;
           else prev->_left = curr->_right;
 
           delete curr;
       }
}
	

#endif


Last edited on
Could you please copy and paste the error message exactly and indicate which line in your code above it refers to?
The code you showed has no one declaration with type int or int *. So you should show the statement that become the reason of the error.
This is the error message

btree.h(76): error C2664: 'Node<T>::find_node' : cannot convert parameter 1 from 'Node<T> *' to 'const int &'
with
[
T=int
]
Reason: cannot convert from 'Node<T> *' to 'const int'
with
[
T=int
]
void find_node( const T &val, const T &root) const;
73
74
75
76
77
template < typename elemType >
inline void BinaryTree< elemType >::remove( const elemType &e )
{
    _root->find_node( _root, e );
}
I think you got the order of parameters backwards.
Last edited on
the remove function is what was given to me by my teacher. are you talking about that being backwards, orthe parameters in my find_node function?
One or the other is backwards - if you switch the order of either, the code works.
really? i tried compiling already after switching, but I was unable to get the code to compile
Instead of const T &root it should probably be a node (and it should be the first parameter)
Not sure how I would do that. Sorry, I am new to this programming. I also think my cpp files would be helpful, but I dont have room in the above code to add it in. Actually, I will add one of the cpp files in because I get another error in the cpp file. I will put here in this comment.
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
#include    "btree.h"
#include    <fstream>
#include    <string>

using namespace std;


ifstream get_ifs( )                             // get input file stream
{
    string filename;                            // input file name

    cerr << "name of file to read from? ";
    cin  >> filename;

    ifstream ifs( filename, ifstream::in );
    if( ! ifs )                                 // cannot open file infilen
    {
        cerr << "cannot open input file '" << filename << "'\n";
        exit( 1 );
    }

    return ifs;                                 // return input file stream
}


int main()
{
    BinaryTree< int > bt;

    bt.insert( 30 );
    bt.insert( 20 );
    bt.insert( 40 );
    bt.insert( 50 );
    bt.insert( 10 );
    bt.insert( 15 );
    bt.insert( 35 );

    cout << "inorder traversal:\n";
    bt.inorder( );

    ifstream ifs = get_ifs( );

    string cmd;
    int    value;

    while( ifs >> cmd )
    {
        ifs >> value;

        cout << cmd << '\t' << value << '\n';

        if( cmd == "a" )
        {
            bt.insert( value );
        }
        else if( cmd == "d" )
        {
            bt.remove( value );
        }
        else
        {
            cout << "invalid command '" << cmd << "' ignored...\n\n";
        }

        bt.inorder( );
        cout << '\n';
    }

    return 0;
}


this is the error message
tree2.cpp(28) : see reference to class template instantiation 'BinaryTree<elemType>' being compiled
with
[
elemType=int
]
Last edited on
Change from:

void find_node( const T &val, const T &root) const;

to this:

void find_node(Node<T> *root, const T &val) const;
Unfortunately, I still receive the same error
Did you also change the definition?
yes, i change the definition, and everywhere else the function was being used.
No, don't change how the function is being used, just change where ever you see that parameter list.
yes, that is what I meant. So i did void find_node(Node<T> *root, const T &val) const;

change it here as well line 165
inline void Node < T> :: find_node( Node<T> *root, const T &val ) const {}

as well as here on line 145
const T * find_node(Node<T> *root, const T &val)
Can you copy and paste the new error(s)? I doubt it's identical.
here are all of the errors that came up. pretty much the same amount as before. I just didn't copy all of them. But here is all of the errors:

tree2.cpp
1>c:\users\owner\documents\visual studio 2010\projects\bst\bst\node.h(168): error C2664: 'Node<T>::find_node' : cannot convert parameter 1 from 'const int' to 'Node<T> *'
1> with
1> [
1> T=int
1> ]
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1> c:\users\owner\documents\visual studio 2010\projects\bst\bst\node.h(167) : while compiling class template member function 'void Node<T>::find_node(Node<T> *,const T &) const'
1> with
1> [
1> T=int
1> ]
1> c:\users\owner\documents\visual studio 2010\projects\bst\bst\btree.h(65) : see reference to class template instantiation 'Node<T>' being compiled
1> with
1> [
1> T=int
1> ]
1> c:\users\owner\documents\visual studio 2010\projects\bst\bst\btree.h(62) : while compiling class template member function 'void BinaryTree<elemType>::insert(const elemType &)'
1> with
1> [
1> elemType=int
1> ]
1> c:\users\owner\documents\visual studio 2010\projects\bst\bst\tree2.cpp(28) : see reference to class template instantiation 'BinaryTree<elemType>' being compiled
1> with
1> [
1> elemType=int
1> ]
1>c:\users\owner\documents\visual studio 2010\projects\bst\bst\node.h(171): error C2660: 'Node<T>::delete_node' : function does not take 2 arguments
1> with
1> [
1> T=int
1> ]
1> tree1.cpp
1>c:\users\owner\documents\visual studio 2010\projects\bst\bst\node.h(168): error C2664: 'Node<T>::find_node' : cannot convert parameter 1 from 'const std::string' to 'Node<T> *'
1> with
1> [
1> T=std::string
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> c:\users\owner\documents\visual studio 2010\projects\bst\bst\node.h(167) : while compiling class template member function 'void Node<T>::find_node(Node<T> *,const T &) const'
1> with
1> [
1> T=std::string
1> ]
1> c:\users\owner\documents\visual studio 2010\projects\bst\bst\btree.h(65) : see reference to class template instantiation 'Node<T>' being compiled
1> with
1> [
1> T=std::string
1> ]
1> c:\users\owner\documents\visual studio 2010\projects\bst\bst\btree.h(62) : while compiling class template member function 'void BinaryTree<elemType>::insert(const elemType &)'
1> with
1> [
1> elemType=std::string
1> ]
1> c:\users\owner\documents\visual studio 2010\projects\bst\bst\tree1.cpp(28) : see reference to class template instantiation 'BinaryTree<elemType>' being compiled
1> with
1> [
1> elemType=std::string
1> ]
1>c:\users\owner\documents\visual studio 2010\projects\bst\bst\node.h(169): error C2451: conditional expression of type 'const std::string' is illegal
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\users\owner\documents\visual studio 2010\projects\bst\bst\node.h(171): error C2660: 'Node<T>::delete_node' : function does not take 2 arguments
1> with
1> [
1> T=std::string
1> ]
Compiler wrote:
1>c:\users\owner\documents\visual studio 2010\projects\bst\bst\node.h(168): error C2664: 'Node<T>::find_node' : cannot convert parameter 1 from 'const int' to 'Node<T> *'
This error is the opposite of what it was before. Are you absolutely sure you did not change the order of parameters where the function was called? Paste your code again, please (I know this is getting boring, sorry)
oh, i didn't realize that one. I'm sure I didn't change anything. I will paste the node.h and btree.h there again. I'll replace it there at the top. also, if this helps, when i sent my code to my professor, he gave this remark back for my find_node function.
Your find_node in your node.h needs to have two arguments.

You've got one (the val that you are searching for), but you should also be receiving the pointer to the node to start searching with.

Initially, that is the root.

find_node should be a binary search, that decends down the left or right side of the tree, executing itself until val == p->value(), when it deletes p and leaves.
Pages: 12