fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class led {
  5. private:
  6. int id;
  7. static int nextid;
  8. public:
  9. led() { id=++nextid; }
  10. int get_id() { return id; }
  11. void turn_on() { cout<<"Turn on led "<<id<<endl; }
  12. void turn_off() { cout<<"Turn off led "<<id<<endl; }
  13. };
  14. class blinking_led : public led {
  15. public:
  16. void blink() { cout<<"Blink "<<get_id()<<endl; cout<<" ->"; turn_on();cout<<" ->"; turn_off();}
  17. };
  18. class auto_switch{
  19. void (led::*action)();
  20. led *ld;
  21. public:
  22. auto_switch(void(led::*a)(), led*l) : action(a), ld(l) {}
  23. void go () { (ld->*action)(); }
  24. };
  25.  
  26. int led::nextid=0;
  27.  
  28. int main() {
  29. led l1;
  30. blinking_led l2;
  31. l1.turn_on(); l1.turn_off();
  32. l2.blink();
  33. auto_switch s(&led::turn_off, &l1);
  34. s.go();
  35. auto_switch s2(&blinking_led::blink, &l2);
  36.  
  37. return 0;
  38. }
Compilation error #stdin compilation error #stdout 0s 4284KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:35:42: error: no matching function for call to ‘auto_switch::auto_switch(void (blinking_led::*)(), blinking_led*)’
  auto_switch s2(&blinking_led::blink, &l2);
                                          ^
prog.cpp:22:2: note: candidate: ‘auto_switch::auto_switch(void (led::*)(), led*)’
  auto_switch(void(led::*a)(), led*l) : action(a), ld(l) {}
  ^~~~~~~~~~~
prog.cpp:22:2: note:   no known conversion for argument 1 from ‘void (blinking_led::*)()’ to ‘void (led::*)()’
prog.cpp:18:7: note: candidate: ‘constexpr auto_switch::auto_switch(const auto_switch&)’
 class auto_switch{
       ^~~~~~~~~~~
prog.cpp:18:7: note:   candidate expects 1 argument, 2 provided
prog.cpp:18:7: note: candidate: ‘constexpr auto_switch::auto_switch(auto_switch&&)’
prog.cpp:18:7: note:   candidate expects 1 argument, 2 provided
stdout
Standard output is empty