I am trying to write a print function which prints in reverse order elements of linked list. It works only when I declare the function non-const with const it does not work and throw below error.
cannot convert 'this' pointer from 'const slist<int>' to 'slist<int> &'
I saw few SO post with regard to it like below Call a non-const member function from a const member function and the associated post with that link but I am unable to understand it. If someone can help me understand it
My code :
Gives error : cannot convert 'this' pointer from 'const slist<int>' to 'slist<int> &'
void slist<T>::print_with_recursion() const
{
node<T>* head = _first;
print_reverse(head);
}
void slist<T>::print_reverse(node<T>* head)
{
if (head)
{
print_reverse(head->_next);
cout << head->_data << endl;
}
}
if I remove const I dont get any error. Also if there is better way to implement printing linked list in reverse order give function definition print_with_recursion() const please do suggest.