Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No matching function for call to 'std::advance' error

Tags:

c++

c++11

I have the following code which Xcode says it doesn't recognised the signature for std::advance:

template<typename Container> const typename Container::value_type&
getNthElement(const Container& container, size_t n) {
    auto nElem = advance(container.begin(), n);
    return *nElem;
}

The compiler doesn't support C++14 so I can't use cbegin(container). Why is this solution wrong ?

like image 759
Adrian Avatar asked Sep 05 '25 01:09

Adrian


1 Answers

std::advance doesn't return anything (see here: http://www.cplusplus.com/reference/iterator/advance/, its return type is void) so auto nElem = advance(container.begin(), n); isn't valid. As said in comments, you can use next. :)

like image 105
vincentp Avatar answered Sep 07 '25 11:09

vincentp