2017-10-18 40 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(而不是一個自由的功能),你需要給線程構造函數的類實例的引用。