2017-10-18 53 views
1

我想按照这个教程http://www.cplusplus.com/reference/thread/thread/C++编译11由于未能与模板和螺纹

创建一个新线程,以下是相关的代码,我写

Game.cpp问题:

Game :: Game (bool isDebug, bool startNew) { 
    // ... 
    std :: thread gameThread(Game :: tick); 
} 

void Game :: tick() { 
    std :: this_thread :: sleep_for(std :: chrono :: seconds (1)); 
    funds += getIncomeRate(); 
} 

Game.hpp:

#ifndef Game_hpp 
#define Game_hpp 

#include <thread> 

using namespace std; 

class Game{ 
    public: 
    // ... 
     Game (bool, bool); 
     void tick(); 
}; 

#endif /* Game_hpp */ 

compil过程中出现了错误ING表示,

17:37:19 **** Incremental Build of configuration Debug for project Store Management Game **** 
Info: Internal Builder is used for build 
g++ -std=c++11 -O0 -g3 -Wall -c -fmessage-length=0 -o Game.o "..\\Game.cpp" 
In file included from C:/Program Files/Haskell Platform/8.0.1/mingw/include/c++/5.2.0/thread:39:0, 
       from ..\Game.hpp:16, 
       from ..\Game.cpp:9: 
C:/Program Files/Haskell Platform/8.0.1/mingw/include/c++/5.2.0/functional: In instantiation of 'struct std::_Bind_check_arity<void (Game::*)()>': 
C:/Program Files/Haskell Platform/8.0.1/mingw/include/c++/5.2.0/functional:1538:12: required from 'struct std::_Bind_simple_helper<void (Game::*)()>' 
C:/Program Files/Haskell Platform/8.0.1/mingw/include/c++/5.2.0/functional:1552:5: required by substitution of 'template<class _Callable, class ... _Args> typename std::_Bind_simple_helper<_Func, _BoundArgs>::__type std::__bind_simple(_Callable&&, _Args&& ...) [with _Callable = void (Game::*)(); _Args = {}]' 
C:/Program Files/Haskell Platform/8.0.1/mingw/include/c++/5.2.0/thread:142:59: required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (Game::*)(); _Args = {}]' 
..\Game.cpp:33:46: required from here 
C:/Program Files/Haskell Platform/8.0.1/mingw/include/c++/5.2.0/functional:1426:7: error: static assertion failed: Wrong number of arguments for pointer-to-member 
     static_assert(_Varargs::value 
    ^
C:/Program Files/Haskell Platform/8.0.1/mingw/include/c++/5.2.0/functional: In instantiation of 'struct std::_Bind_simple<std::_Mem_fn<void (Game::*)()>()>': 
C:/Program Files/Haskell Platform/8.0.1/mingw/include/c++/5.2.0/thread:142:59: required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (Game::*)(); _Args = {}]' 
..\Game.cpp:33:46: required from here 
C:/Program Files/Haskell Platform/8.0.1/mingw/include/c++/5.2.0/functional:1505:61: error: no type named 'type' in 'class std::result_of<std::_Mem_fn<void (Game::*)()>()>' 
     typedef typename result_of<_Callable(_Args...)>::type result_type; 
                  ^
C:/Program Files/Haskell Platform/8.0.1/mingw/include/c++/5.2.0/functional:1526:9: error: no type 
named 'type' in 'class std::result_of<std::_Mem_fn<void (Game::*)()>()>' 
     _M_invoke(_Index_tuple<_Indices...>) 
     ^

17:37:20 Build Finished (took 583ms) 

我不是一般很熟悉C++和模板,我真不明白什么是错我的代码。如果这是一个微不足道的问题,请提前道歉。

回答

1

你想:

std::thread gameThread(&Game::tick, this); 
//     ^^^   ^^^^ 
1

既然你包装成一个线程的方法是不static(而不是一个自由的功能),你需要给线程构造函数的类实例的引用。