template<class T>
class BNode
{
public:
T data;
BNode *parent, *left, *right;
explicit BNode(T d) : data(d), parent(nullptr), left(nullptr),
right(nullptr)
{}
};
Problem 1: This is right.
template<class T>
void BST<T>::InOrder_(BNode<T> *&s)
{
if (s)
{
InOrder_(s->left);
cout << s->data << ' ';
InOrder_(s->right);
}
}
Problem 2: This is wrong.
template<class T>
void BST<T>::InOrder_(const BNode<T> *&s)
{
if (s)
{
InOrder_(s->left);
cout << s->data << ' ';
InOrder_(s->right);
}
}
error: cannot bind non-const lvalue reference of type 'const BNode<int>*&
' to an rvalue of type 'const BNode<int>*
'
InOrder_(s->left);
~~~^~~~
Problem 3: This is right.
template<class T>
void BST<T>::InOrder_(BNode<T> *const &s)
{
......
}
Problem 4: This is right.
template<class T>
void BST<T>::InOrder_(const BNode<T> *const &s)
{
......
}
Can you tell me the above problems why is right or wrong? Thank you!