I have the below five files.
I am trying to call a function in two_b.cpp from one_a.cpp using a std::function callback, but I get the following error:
terminate called after throwing an instance of 'std::bad_function_call'
  what():  bad_function_call
Aborted (core dumped)
one_a.h
#include <functional>
using CallbackType = std::function<void()>;
class Car {
  public:
    void car_run(void);
    void car_stop(void);
    CallbackType f1;
private:
  CallbackType callbackHandler;
};
one_a.cpp
#include "one_a.h"
#include <iostream>
void Car::car_run(void)
{
    std::cout<<"Car running"<<std::endl;
}
void Car::car_stop(void)
{
    std::cout<<"Car stopping"<<std::endl;
}
two_b.h
#include "one_a.h"
class Boat {
  public:
    Boat(Car& car_itf);
    static void boat_run(void);
    static void boat_stop(void);
 private:
    Car& car_itf_;
};
two_b.cpp
#include "two_b.h"
#include <iostream>
Boat::Boat(Car& car_itf):car_itf_{car_itf}{
    car_itf_.f1 = std::bind(&Boat::boat_run);
}
void Boat::boat_run(void)
{
    std::cout<<"Boat running"<<std::endl;
}
void Boat::boat_stop(void)
{
    std::cout<<"Boat running"<<std::endl;
}
main.cpp
#include "two_b.h"
int main()
{
    Car bmw;
    bmw.car_run();
    bmw.f1();
    return 0;
}
A running example can be found here:
 
    