I am training using lambda function and I create a small function that merge two lists using template class. I want to use lambda function in order to compare age and merge them into a new list that will be in ascending order.
Compiler tell that no instance of function template "merge_func" matches the argument list
.
If you can help me understand what is wrong in my lambda function, and how can I correct it.
template <class T>
list<T*> merge_func(list<T*> first_list, list<T*> second_list, bool(*func)(T x, T y))
{
list<T> merge_list;
auto it_first = first_list.begin();
auto it_second = second_list.begin();
while (it_first != first_list.end() && it_second != second_list.end())
{
if ((*func)(*it_first, *it_second))
{
merge_list.push_back((*it_first));
it_first++;
}
else
{
merge_list.push_back((*it_second));
it_second++;
}
}
while (it_first != first_list.end())
{
merge_list.push_back((*it_first));
it_first++;
}
while (it_second != second_list.end())
{
merge_list.push_back((*it_second));
it_second++;
}
return merge_list;
}
class Student
{
public:
string _name;
double _age;
Student(string name, double age) : _name(name), _age(age) {};
};
int main()
{
list<Student*> std;
std.push_back(new Student("Lior", 15.5));
std.push_back(new Student("Yossi", 60));
list<Student*> std2;
std2.push_back(new Student("Arie", 23));
std2.push_back(new Student("Eli", 80));
list<Student*> std3;
std3 = merge_func(std, std2, [](Student* x, Student* y)->bool {return x->_age < y->_age; });