2016-09-28 36 views
-1

大家好,希望大家都很好。我正在尝试使用C++中的线程。我编译了代码,并得到以下错误消息。尝试与线程一起工作时出错

C:\Users\Peretz\Documents\keylogger\KeyConstants.h|183|warning: unknown escape sequence: '\|' [enabled by default]| 
C:\Users\Peretz\Documents\keylogger\Timer.h|10|error: 'thread' in namespace 'std' does not name a type| 
C:\Users\Peretz\Documents\keylogger\Timer.h||In member function 'void Timer::SleepAndRun()':| 
C:\Users\Peretz\Documents\keylogger\Timer.h|25|error: 'std::this_thread' has not been declared| 
C:\Users\Peretz\Documents\keylogger\Timer.h||In member function 'void Timer::Start(bool)':| 
C:\Users\Peretz\Documents\keylogger\Timer.h|71|error: 'Thread' was not declared in this scope| 
C:\Users\Peretz\Documents\keylogger\Timer.h|71|error: 'thread' is not a member of 'std'| 
C:\Users\Peretz\Documents\keylogger\Timer.h||In member function 'void Timer::Stop()':| 
C:\Users\Peretz\Documents\keylogger\Timer.h|82|error: 'Thread' was not declared in this scope| 
||=== Build failed: 5 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===| 

我在Youtube上查找了C++中的线程和C++ 11中的线程,但找不到答案。请解释为什么有这些错误。

#ifndef TIMER_H 
#define TIMER_H 

#include <thread> 
#include <chrono> 

class Timer 
{  
    std::thread Thread; 
    bool Alive = false; 
    long CallNumber = -1L; 

    long repeat_count = -1L; 
    std::chrono::milliseconds interval = std::chrono::milliseconds(0); 
    std::function<void(void)> funct = nullptr; 

    void SleepAndRun() 
    { 
     std::this_thread::sleep_for(interval); 
     if (Alive) 
     { 
      Function()(); 
     } 
    } 

    void ThreadFunc() 
    { 
     if (CallNumber == Infinite) 
      while (Alive) 
       SleepAndRun(); 
     else 
      while (repeat_count--) 
       SleepAndRun(); 
    } 

    public: 

    static const long Infinite = -1L; 

    Timer() {} 
    Timer(const std::function<void(void)> &f) : funct (f) {} 
    Timer(const std::function<void(void)> &f, 
      const unsigned long &i, 
      const long repeat = Timer::Infinite) : funct (f), 
     interval (std::chrono::milliseconds(i)), CallNumber (repeat) {} 

    void Start (bool Async = true) 
    { 
     if (IsAlive()) 
      return; 
     Alive = true; 
     repeat_count = CallNumber; 
     if (Async) 
      Thread = std::thread (ThreadFunc,this); 
     else 
      this->ThreadFunc(); 
    } 

    void Stop() 
    { 
     Alive = false; 
     Thread.join(); 
    } 

    void SetFunction (const std::function<void (void)> &f) 
    { 
     funct = f; 
    } 

    bool IsAlive() const 
    { 
     return Alive; 
    } 

    void RepeatCount (const long r) 
    { 
     if (Alive) 
      return; 
     CallNumber = r; 
    } 

    long GetLeftCount() const 
    { 
     return repeat_count; 
    } 

    long RepeatCount() const 
    { 
     return CallNumber; 
    } 

    void SetInterval (const unsigned long &i) 
    { 
     if (Alive) 
      return; 
     interval = std::chrono::milliseconds(i); 
    } 

    unsigned long Interval() const 
    { 
     return interval.count(); 
    } 

    const std::function<void(void)> &Function() const 
    { 
     return funct; 
    } 
}; 
#endif 

非常感谢。

+4

您是否在编译器中启用了C++ 11? – NathanOliver

+1

而你的命令来运行这个是? ... – amanuel2

+0

另外,添加'#include '以支持'std :: function'。 –

回答

0
error: 'thread' in namespace 'std' does not name a type 

你使用哪种编译器(看起来像的Visual C++)不支持std::thread(这是C++ 11的部分)。