I have a container object :
R Container;
R is of type list<T*> or vector<T*>
I am trying to write the following function:
template<typename T, typename R>
T& tContainer_t<T, R>::Find( T const item ) const
{
typename R::const_iterator it = std::find_if(Container.begin(), Container.end(), [item](const R&v) { return item == v; });
if (it != Container.end())
return (**it);
else
throw Exception("Item not found in container");
}
When trying the method (v is an object of my class)
double f = 1.1;
v.Find(f);
I get binary '==' : no operator found which takes a left-hand operand of type 'const double' (or there is no acceptable conversion)
I am confused with the lambda expression syntax and what i should write there and couldn't find any friendly explanation.
What is wrong ? 10x.
Missing a bit of context, but I note:
**it so you possibly want to compare *v==itemtconst R&v where I suspect you meant const T&v into the lambdaHere is working code, stripped of the missing class references:
#include <vector>
#include <algorithm>
#include <iostream>
template<typename T, typename R=std::vector<T> >
T& Find(R& Container, T const& item )
{
typename R::iterator it = std::find_if(Container.begin(), Container.end(), [&item](const T&v) { return item == v; });
if (it != Container.end())
return *it;
else
throw "TODO implement";
}
int main(int argc, const char *argv[])
{
std::vector<double> v { 0, 1, 2, 3 };
Find(v, 2.0); // not '2', but '2.0' !
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With