F
F
Fat Lorrie2016-05-02 12:31:01
C++ / C#
Fat Lorrie, 2016-05-02 12:31:01

Why does vector::end() return a const_iterator?

class SomeType{
    std::vector<AnotherType> _items;
public:
    void removeItem(AnotherType *pItem);
};
void SomeType::removeItem(AnotherType *pItem) {
    auto found = std::find(_items.begin(), _items.end(), *pItem); // error
    //....
}

GCC swears at the line with the search:
C:\Qt\Tools\mingw492_32\i686-w64-mingw32\include\c++\bits\predefined_ops.h:191: ошибка: no match for 'operator==' (operand types are 'AnotherType' and 'const AnotherType')
  { return *__it == _M_value; }
                 ^

Why is end overload allowed if _items is not a constant vector?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MiiNiPaa, 2016-05-02
@Free_ze

And where did you get the idea that end returns a const_iterator? If it returned a constant iterator, then the types in error would be 'const AnotherType' and 'const AnotherType'
Correct the comparison operator so that it takes objects by constant reference.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question