#include <iostream>
#include <functional>
class Solver{
public:
void print(){
std::cout << "i solved" << std::endl;
}
};
class ThingHandler{
public:
void handleThing(Solver& solver){
std::cout << "i handled something " << std::endl;
solver.print();
}
};
class CantHandle{
public:
void needHelp(std::function<void(Solver&)> handleThing){
handleThing(); // how do i here pass Solver as a parameter?
}
};
int main() {
ThingHandler thingHandler;
CantHandle cantHandle;
Solver solver;
auto fp = std::bind(&ThingHandler::handleThing, thingHandler,solver);
cantHandle.needHelp(fp);
return 0;
}
In the class "CantHandle" when i call the helper function it asks for an parameter. This makes sense since the function that's being called is handleThing which takes a ref to a solver as parameter. But i can't seem to figure out the syntax in the class ThingHandler.Should not handleThing already have the parameter bound? I'm getting the following error "no match for call to ‘(std::function) ()"
Full error message:
error: no match for call to ‘(std::function) ()’ 23 |
handleThing(); // how do i here pass Solver as a parameter? note: candidate: ‘_Res std::function<_Res(_ArgTypes ...)>::operator()(_ArgTypes ...) const [with _Res = void; _ArgTypes = {Solver&}]’ 685 | function<_Res(_ArgTypes...)>:: |
^~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/include/c++/9/bits/std_function.h:685:5: note: candidate expects 1 argument, 0 provided gmake[3]: * [CMakeFiles/funcObj.dir/build.make:63: CMakeFiles/funcObj.dir/main.cpp.o] Error 1 gmake[2]: [CMakeFiles/Makefile2:73: CMakeFiles/funcObj.dir/all] Error 2 gmake[1]: [CMakeFiles/Makefile2:85: CMakeFiles/funcObj.dir/rule] Error 2 gmake: * [Makefile:118: funcObj] Error 2