C++ “no match for operator=“ error in lambda function

I have a lambda function in a non-static class member function, used in std::find.

//Somewhere in the header...
Gtk::ComboBoxText *optionChoiceComboBoxText = nullptr; // Initialized later in code
std::vector<OptionData> optionList;
int currentOptionPositionInList = -1; 

// In Interface.cpp
Interface::optionChangedHandler() {
currentOptionPositionInList = std::distance(optionList.begin(),
                                            find(optionList.begin(), optionList.end(), [&](OptionData const& o) {
                                                std::string s = optionChoiceComboBoxText->get_active_text();
                                                return s == o.name;
                                            })); //Error is here
}

Here is the error with notes:

[ 66%] Building CXX object CMakeFiles/RNS.dir/src/GUI/Interface.cpp.o
In file included from /usr/include/c++/5/bits/stl_algobase.h:71:0,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iterator:64,
                 from /usr/include/glibmm-2.4/glibmm/ustring.h:29,
                 from /usr/include/glibmm-2.4/glibmm/exception.h:25,
                 from /usr/include/glibmm-2.4/glibmm/error.h:23,
                 from /usr/include/glibmm-2.4/glibmm/thread.h:49,
                 from /usr/include/glibmm-2.4/glibmm.h:87,
                 from /usr/include/giomm-2.4/giomm.h:23,
                 from /home/crefrod/RNS/src/GUI/Interface.cpp:5:
/usr/include/c++/5/bits/predefined_ops.h: In instantiation of ‘bool __gnu_cxx::__ops::_Iter_equals_val<_Value>::operator()(_Iterator) [with _Iterator = __gnu_cxx::__normal_iterator<OptionData*, std::vector<OptionData> >; _Value = const Interface::optionChangedHandler()::<lambda(const OptionData&)>]’:
/usr/include/c++/5/bits/stl_algo.h:120:14:   required from ‘_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<OptionData*, std::vector<OptionData> >; _Predicate = __gnu_cxx::__ops::_Iter_equals_val<const Interface::optionChangedHandler()::<lambda(const OptionData&)> >]’
/usr/include/c++/5/bits/stl_algo.h:161:23:   required from ‘_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = __gnu_cxx::__normal_iterator<OptionData*, std::vector<OptionData> >; _Predicate = __gnu_cxx::__ops::_Iter_equals_val<const Interface::optionChangedHandler()::<lambda(const OptionData&)> >]’
/usr/include/c++/5/bits/stl_algo.h:3790:28:   required from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator<OptionData*, std::vector<OptionData> >; _Tp = Interface::optionChangedHandler()::<lambda(const OptionData&)>]’
/home/crefrod/RNS/src/GUI/Interface.cpp:70:50:   required from here
/usr/include/c++/5/bits/predefined_ops.h:194:17: error: no match for ‘operator==’ (operand types are ‘OptionData’ and ‘const Interface::optionChangedHandler()::<lambda(const OptionData&)>’)
  { return *__it == _M_value; }
                 ^
In file included from /usr/include/c++/5/iterator:63:0,
                 from /usr/include/glibmm-2.4/glibmm/ustring.h:29,
                 from /usr/include/glibmm-2.4/glibmm/exception.h:25,
                 from /usr/include/glibmm-2.4/glibmm/error.h:23,
                 from /usr/include/glibmm-2.4/glibmm/thread.h:49,
                 from /usr/include/glibmm-2.4/glibmm.h:87,
                 from /usr/include/giomm-2.4/giomm.h:23,
                 from /home/crefrod/RNS/src/GUI/Interface.cpp:5:
/usr/include/c++/5/bits/stl_iterator.h:820:5: note: candidate: template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator==(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)
     operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
     ^
/usr/include/c++/5/bits/stl_iterator.h:820:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/5/bits/stl_algobase.h:71:0,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iterator:64,
                 from /usr/include/glibmm-2.4/glibmm/ustring.h:29,
                 from /usr/include/glibmm-2.4/glibmm/exception.h:25,
                 from /usr/include/glibmm-2.4/glibmm/error.h:23,
                 from /usr/include/glibmm-2.4/glibmm/thread.h:49,
                 from /usr/include/glibmm-2.4/glibmm.h:87,
                 from /usr/include/giomm-2.4/giomm.h:23,
                 from /home/crefrod/RNS/src/GUI/Interface.cpp:5:
/usr/include/c++/5/bits/predefined_ops.h:194:17: note:   ‘OptionData’ is not derived from ‘const __gnu_cxx::__normal_iterator<_IteratorL, _Container>’
  { return *__it == _M_value; }
                 ^
In file included from /usr/include/c++/5/iterator:63:0,
                 from /usr/include/glibmm-2.4/glibmm/ustring.h:29,
                 from /usr/include/glibmm-2.4/glibmm/exception.h:25,
                 from /usr/include/glibmm-2.4/glibmm/error.h:23,
                 from /usr/include/glibmm-2.4/glibmm/thread.h:49,
                 from /usr/include/glibmm-2.4/glibmm.h:87,
                 from /usr/include/giomm-2.4/giomm.h:23,
                 from /home/crefrod/RNS/src/GUI/Interface.cpp:5:
/usr/include/c++/5/bits/stl_iterator.h:827:5: note: candidate: template<class _Iterator, class _Container> bool __gnu_cxx::operator==(const __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&)
     operator==(const __normal_iterator<_Iterator, _Container>& __lhs,
     ^
/usr/include/c++/5/bits/stl_iterator.h:827:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/5/bits/stl_algobase.h:71:0,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iterator:64,
                 from /usr/include/glibmm-2.4/glibmm/ustring.h:29,
                 from /usr/include/glibmm-2.4/glibmm/exception.h:25,
                 from /usr/include/glibmm-2.4/glibmm/error.h:23,
                 from /usr/include/glibmm-2.4/glibmm/thread.h:49,
                 from /usr/include/glibmm-2.4/glibmm.h:87,
                 from /usr/include/giomm-2.4/giomm.h:23,
                 from /home/crefrod/RNS/src/GUI/Interface.cpp:5:
/usr/include/c++/5/bits/predefined_ops.h:194:17: note:   ‘OptionData’ is not derived from ‘const __gnu_cxx::__normal_iterator<_Iterator, _Container>’
  { return *__it == _M_value; }
                 ^
In file included from /usr/include/x86_64-linux-gnu/c++/5/bits/c++allocator.h:33:0,
                 from /usr/include/c++/5/bits/allocator.h:46,
                 from /usr/include/c++/5/string:41,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iterator:64,
                 from /usr/include/glibmm-2.4/glibmm/ustring.h:29,
                 from /usr/include/glibmm-2.4/glibmm/exception.h:25,
                 from /usr/include/glibmm-2.4/glibmm/error.h:23,
                 from /usr/include/glibmm-2.4/glibmm/thread.h:49,
                 from /usr/include/glibmm-2.4/glibmm.h:87,
                 from /usr/include/giomm-2.4/giomm.h:23,
                 from /home/crefrod/RNS/src/GUI/Interface.cpp:5:
/usr/include/c++/5/ext/new_allocator.h:139:5: note: candidate: template<class _Tp> bool __gnu_cxx::operator==(const __gnu_cxx::new_allocator<_Tp>&, const __gnu_cxx::new_allocator<_Tp>&)
     operator==(const new_allocator<_Tp>&, const new_allocator<_Tp>&)
     ^
/usr/include/c++/5/ext/new_allocator.h:139:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/5/bits/stl_algobase.h:71:0,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iterator:64,
                 from /usr/include/glibmm-2.4/glibmm/ustring.h:29,
                 from /usr/include/glibmm-2.4/glibmm/exception.h:25,
                 from /usr/include/glibmm-2.4/glibmm/error.h:23,
                 from /usr/include/glibmm-2.4/glibmm/thread.h:49,
                 from /usr/include/glibmm-2.4/glibmm.h:87,
                 from /usr/include/giomm-2.4/giomm.h:23,
                 from /home/crefrod/RNS/src/GUI/Interface.cpp:5:
/usr/include/c++/5/bits/predefined_ops.h:194:17: note:   ‘OptionData’ is not derived from ‘const __gnu_cxx::new_allocator<_Tp>’
  { return *__it == _M_value; }

So it looks like the compiler tries to assign OptionData to the lambda itself (or what is ‘const Interface::optionChangedHandler()::<lambda(const OptionData&)>’). But I do not understand where does OptionData come from, because both OptionData.name and s are std::string's.

1 answer

  • answered 2018-01-11 19:56 CoryKramer

    You need to replace std::find with std::find_if.

    std::find has the signature find( InputIt first, InputIt last, const T& value ), it thinks you are passing your lambda as a type T, which is why it is trying to convert your lambda to a OptionData and getting upset