So I'm writing a data structure from scratch as part of a university assignment in c++, and I have to write an iterator for it. The problem involves comparison between constant iterators and normal iterators, and I'm going about it in this way: I wrote a constructor of normal iterator which takes a const iterator as its only parameter, hoping that the comparison operator between two normal iterators will be enough: btree_iterator(const_btree_iterator<T>&conv):_target(conv._target),_index(conv._index),_last(conv._last){}
btree<int> bl(5);//the data structure
auto iter = bl.begin();
iter != bl.cend(); //trying to compare iterator with const iterator
but apparently this is wrong, since the compiler tells me something along the line of "no function 'operator!=' which takes blah blah.." It seems the constructor is alright, since the following works:
btree<int>::iterator i(bl.cend());
Could you please help me understand this? Am I getting something fundamentally wrong about it? How is this functionality actually implemented in C++ library containers like vector? Thank you in advance.
sure I can, and that's what I did at one point, but since the two classes are very much the same, I ended up with a working program that has a lot of duplicated code. I'm still curious why the above approach is wrong though. By the way, is having overloaded comparison functions the way it's implemented in the c++ library? Thanks.
Where are the template functions defined? They have to be defined in the header, not in their own .cpp file. See "Templates and multiple-file projects" at the bottom of this page:
@doug4: in that case the error would be `undefined reference'
@OP
> something along the line of "no function 'operator!=' which takes blah blah..
Don't paraphrase the message if you don't understand it.
btree_iterator(const_btree_iterator<T>&conv) you ask for a non-const reference ¿is there a good reason to that?
cbegin() would return a temporary, which cannot be bind to a non-const reference
Change your constructor to btree_iterator(const const_btree_iterator<T>&conv)