I'm trying to add all items in one list to another using the first list's forEach
method that takes a function pointer and calls the function on each element, but I'm having trouble creating a function pointer that can call the second list's member functions. I've tried using lambdas:
LinearLinkedList<bar> test;
//<add items to test>
LinearLinkedList<bar> sorted;
auto addToSorted = [&](bar * b) { sorted.addSorted(b, &barLessThan); };
test.forEach(&addToSorted);
and using an inner struct
:
LinearLinkedList<bar> test;
//<add items to test>
LinearLinkedList<bar> sorted;
struct Sorter {
static LinearLinkedList<bar> * list = &sorted;
static void addToSorted(bar * b) { list->addSorted(b, &barLessThan); }
};
test.forEach(&Sorter::addToSorted);
but both are rejected by the compiler because they reference sorted
in an illegal way. How can I reference sorted
in a call to forEach
?