Hi Folks,

Here is another noob question. I am trying to write a simple predicate for the find_if function in STL. Here is my code

#include<iostream>
#include<vector>
#include<algorithm>
#include<iterator>

using namespace std;

class InRange{
        const int& low;
        const int& high;
public:
        InRange(const int& l, const int& h): low(l), high(h){}
        bool operator() (const int& myValue){return (myValue > low) && (myValue < high);}
};

int main(){
        vector<int> myVector(8);
        myVector.push_back(10);
        myVector.push_back(15);
        myVector.push_back(20);
        myVector.push_back(25);
        myVector.push_back(30);

        vector<int>::iterator myIterator;
        copy(myVector.begin(), myVector.end(), ostream_iterator<int> (cout," "));
        cout<<endl;

        myIterator = find(myVector.begin(), myVector.end(), InRange(10,20));
        cout<<"Found "<<*myIterator<<endl;
        cout<<endl;
}

However I am making some obvious mistake as the code just does not compile. Here is the compiler error. Any ideas of what I am doing wrong. Sorry for this huge error message but for some reason all the compiler messages that I get for STL related errors tend to be very verbose with actually telling me what the error is

In file included from /usr/include/c++/4.4/algorithm:62,
                 from test.cpp:3:
/usr/include/c++/4.4/bits/stl_algo.h: In function ‘_RandomAccessIterator std::__find(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, _Tp = InRange]’:
/usr/include/c++/4.4/bits/stl_algo.h:4224:   instantiated from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, _Tp = InRange]’
test.cpp:31:   instantiated from here
/usr/include/c++/4.4/bits/stl_algo.h:174: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >]() == __val’
/usr/include/c++/4.4/bits/stl_algo.h:4224:   instantiated from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, _Tp = InRange]’
test.cpp:31:   instantiated from here
/usr/include/c++/4.4/bits/stl_algo.h:178: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >]() == __val’
/usr/include/c++/4.4/bits/stl_algo.h:182: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >]() == __val’
/usr/include/c++/4.4/bits/stl_algo.h:186: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >]() == __val’
/usr/include/c++/4.4/bits/stl_algo.h:194: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >]() == __val’
/usr/include/c++/4.4/bits/stl_algo.h:198: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >]() == __val’
/usr/include/c++/4.4/bits/stl_algo.h:202: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >]() == __val’

**

myIterator = find(myVector.begin(), myVector.end(), InRange(10,20));

find();

...
shoudn't it be find_if?

myIterator = find_if(myVector.begin(), myVector.end(), InRange(10,20));
**

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.