So I have a function called find, which has two versions:
template <typename T>
typename btree<T>::iterator btree<T>::find(const T& elem)
{
//Implementation
}
and the other is the const_iterator version:
template <typename T>
typename btree<T>::const_iterator btree<T>::find(const T& elem) const
{
//Implementation
}
In my test file when I do
btree<char>::iterator it = myTree.find('M');
Everything works fine, however when I use the const_iterator version:
btree<char>::const_iterator it = myTree.find('M');
It gives me the error
error: conversion from ‘btree_iterator’ to non-scalar type ‘const_btree_iterator’ requested
Which obviously means that find is only ever using the iterator (non const) version. I know that C++ is supposed to call the const_iterator version automatically – if I had done everything right. So the question is, what might I be doing wrong?
The iterator classes are:
class btree_iterator and class const_btree_iterator which is just a copy paste of btree_iterator with the names changed
Here are the full source code:
btree_iterator.h (includes const_btree_iterator) http://pastebin.com/zQnj9DxA
btree.h http://pastebin.com/9U5AXmtV
btree.tem http://pastebin.com/U9uV3uXj
All the standard containers implement conversion of non-const to const iterators (as specified in the requirements for the Container concept):
You need conversion constructor like so:
I threw in the assignment operator too but I suppose it is redundant