error C2663: : 2 overloads have no legal conversion for 'this' pointer

Ok I have a class with overloaded functions, and I keep getting this:Error 1 error C2663: 'BSearch<T>::inorder' : 2 overloads have no legal conversion for 'this' pointer

but I have checked the class and the functions and as far as I can tell they match like they are supposed to. here's the class

template <typename T>
class BSearch
{
public:
BSearch ();
~BSearch();
void destroy();
void insert(const T & item);
void inorder() const;
void preorder() const;
void postorder() const;
void deleteNode (const T & item);
T * search (const T & item) const;
int height() const;

private:
Node <T> * r;
T * search (Node<T> * & r, const T & item) const;
int height(Node<T> * & r ) const;
void destroy(Node<T> * & r, T & item);
void insert(Node<T> * & r, const T & item);
void deleteNode (Node <T> * & r, const T & item);
void inorder(Node <T> * & r) ;
void preorder(Node <T> * & r) ;
void postorder(Node <T> * & r) ;
};


and here's the functions:

template <typename T>
void BSearch<T>::inorder (Node <T> * & r)
{
if ( r!=0)
{
inorder (r->left);
cout << r->data;
inorder (r->right);
cout << r -> data;
}
}

template <typename T>
void BSearch<T>::inorder () const
{
inorder (r);
}

It's const correctness issues.

const member functions can not change member vars, and they can only call other const member functions. A const function cannot call a nonconst function on the same object.

Here, you have inorder() (a const function) calling inorder(Node<T>*&) (a non-const function). This is not allowed, hence the error.

The solution here is to make both inorder() functions const.

Also...

1
2
3
4
5
template <typename T>
void BSearch<T>::inorder () const
{
inorder (r);
}


Since this is const (because the function is const), that means the member 'r' will be const as well, which means you won't be able to pass it as a nonconst reference. A solution here would be to change inorder:

1
2
3
4
5
//template <typename T>
//void BSearch<T>::inorder (Node <T> * & r)   // bad

template <typename T>
void BSearch<T>::inorder (const Node <T> *r) const  // good 


Notice I make the function const, and I pass just a const pointer, not a non-const reference to a pointer (no need for the reference here).



Also... it's a really bad idea to name your member 'r'. It's not very descriptive at all. It's especially confusing since you use 'r' for all your parameter names.
Topic archived. No new replies allowed.