I'm trying to pass a callback to a template function, but GCC gives me
error: no matching function for call to ‘Test::iterate(main(int, char**)::<anonymous struct>&)’
Why doesn't this work? (Also, for reasons beyond my control, I can't use C++11.)
I've also tried naming the struct e.g. myvis
and calling test.iterate<myvis>(visitor)
, but that didn't work either.
#include <deque>
#include <iostream>
class Test {
public:
std::deque<int> d;
template <typename C>
void iterate(C& c) {
for(std::deque<int>::iterator itr = d.begin(); itr != d.end(); itr++) {
c(*itr);
}
}
};
int main(int argc, char** argv) {
Test test;
test.d.push_back(1);
test.d.push_back(2);
struct {
void operator()(int x) {
std::cout << x << std::endl;
}
} visitor;
test.iterate(visitor);
}