Ubuntu 来自类not函数的计时计时器类传递方法

Ubuntu 来自类not函数的计时计时器类传递方法,ubuntu,chrono,Ubuntu,Chrono,我试图利用tutorialspoint中的代码,但当我想将函数放入类中时,我遇到了一个问题 #include <functional> #include <chrono> #include <future> #include <cstdio> class later { public: template <class callable, class... arguments> later(int after

我试图利用tutorialspoint中的代码,但当我想将函数放入类中时,我遇到了一个问题

#include <functional>
#include <chrono>
#include <future>
#include <cstdio>
class later {
   public:
      template <class callable, class... arguments>
      later(int after, bool async, callable&& f, arguments&&... args){
      std::function<typename std::result_of<callable(arguments...)>::type()> task(std::bind(std::forward<callable>(f), std::forward<arguments>(args)...));
      if (async) {
         std::thread([after, task]() {
            std::this_thread::sleep_for(std::chrono::milliseconds(after));
            task();
         }).detach();
      } else {
         std::this_thread::sleep_for(std::chrono::milliseconds(after));
         task();
      }
   }
};
void test1(void) {
   return;
}

int main() {
   later later_test1(3000, false, &test1);
   later later_test2(1000, false, &test2, 75);
   later later_test3(3000, false, &test2, 101);
}
#包括
#包括
#包括
#包括
下课{
公众:
模板
稍后(int-after、bool-async、可调用和函数、参数和参数){
std::函数任务(std::bind(std::forward(f)、std::forward(args)…);
如果(异步){
线程([之后,任务](){
std::this_线程::sleep_for(std::chrono::毫秒(之后));
任务();
}).detach();
}否则{
std::this_线程::sleep_for(std::chrono::毫秒(之后));
任务();
}
}
};
无效测试1(无效){
返回;
}
int main(){
稍后_test1(3000、false和test1);
稍后的测试2(1000,假,测试2,75);
稍后的测试3(3000,false和测试2,101);
}
我想把函数test1放在同一个类中,或者定义一个新的类。问题是代码无法编译

 #include <functional>
    #include <chrono>
    #include <future>
    #include <cstdio>
    class later {
       public:
          template <class callable, class... arguments>
          later(int after, bool async, callable&& f, arguments&&... args){
          std::function<typename std::result_of<callable(arguments...)>::type()> task(std::bind(std::forward<callable>(f), std::forward<arguments>(args)...));
          if (async) {
             std::thread([after, task]() {
                std::this_thread::sleep_for(std::chrono::milliseconds(after));
                task();
             }).detach();
          } else {
             std::this_thread::sleep_for(std::chrono::milliseconds(after));
             task();
          }
       }

    void test1(void) {
       return;
    }
    };
int main() {
   later later_test1(3000, false, &later::test1);
   later later_test2(1000, false, &test2, 75);
   later later_test3(3000, false, &test2, 101);
}    
#包括
#包括
#包括
#包括
下课{
公众:
模板
稍后(int-after、bool-async、可调用和函数、参数和参数){
std::函数任务(std::bind(std::forward(f)、std::forward(args)…);
如果(异步){
线程([之后,任务](){
std::this_线程::sleep_for(std::chrono::毫秒(之后));
任务();
}).detach();
}否则{
std::this_线程::sleep_for(std::chrono::毫秒(之后));
任务();
}
}
无效测试1(无效){
返回;
}
};
int main(){
later_test1(3000,false,&later::test1);
稍后的测试2(1000,假,测试2,75);
稍后的测试3(3000,false和测试2,101);
}    

在您的第二位代码中,
test1
是稍后版本的非静态成员函数。这意味着它需要一个对象才能被调用

将其设置为静态:

    static void test1(void) {
       return;
    }