Assume that class C contains a function doSomething that is called repeatedly and its results are mandatory for a bunch of other functions of different classes. So in this case the function start of class B. This should operate from another class C. I think that should be pretty easy with std::bind.
But this only works if doSomething belongs to C. If I'm calling it from another class I'm getting a linker error. What am I missing?
#include <iostream>
#include <functional>
class A {
public:
A() {}
template<typename Callable>
void doSomething(Callable f);
};
template<typename Callable>
void A::doSomething(Callable f) {
// do some stuff
int input = 1;
f(input); // B::start should be called here
}
class B {
public:
B() {}
void start(int input);
};
void B::start(int input) {
std::cout << "dostuff" << std::endl;
// do some other stuff
}
class C {
private:
A a;
public:
C() : a() {
B b;
a.doSomething(std::bind(&B::start, b, std::placeholders::_1));
// if doSomething would be a member of class C then
// doSomething(std::bind(&B::start, b, std::placeholder//::_1) would work
}
};
int main() {
C c;
}
What am I missing?
Undefined symbols for architecture x86_64:
"void A::doSomething<std::_Bind<void (B::*(B))()> >(std::_Bind<void (B::*(B))()>)", referenced from:
C::C(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) in C.cpp.o
C::C(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) in C.cpp.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status