c++ automatic conversion for function parameters: when and where does it happen?

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){}

(and somewhere else)

1
2
3
4
template <typename T>
bool operator!=(btree_iterator<T> one,btree_iterator<T> other){
return !(other == one);
}

and I'm testing it in this way:

1
2
3
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.
Last edited on
Can you define overloaded comparisons that take const/non-const iterators? I expect the test for equality to be trivial, right?
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.
Last edited on
I guess, it attempt the same, except I'd make the const_iterator have a constructor that took a mutable iterator.

The key though, is "no function 'operator!=' which takes blah blah..". You need to work thru it to identify out what you've done wrong.
My guess is you never defined btree_iterator<T>::operator!=(), which is what line 3 of your first code segment is trying to do.

I suspect what you want to do is change line three to something like:
return !(*other == *one);

This would compare the contents pointed to by the iterators rather than the iterators themselves.
I do have the not-equal defined, as in the first code segment.
Sorry. My typo. Do you have the btree_iterator<T>::operator==() defined? That's what's being called in line 3.
sure it has, but the issue here is that the compiler doesn't even direct that call to the != function, not to mention the subsequent ==
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:

http://www.cplusplus.com/doc/tutorial/templates/
@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)
Topic archived. No new replies allowed.