由于模板和线程问题,c ++ 11编译失败(c++11 compile failed due to issues with templates and thread)
我正在尝试按照本教程http://www.cplusplus.com/reference/thread/thread/创建一个新线程
以下是我写的相关代码
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 */我在编译期间得到的错误说
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 ++和模板,我真的不明白我的代码有什么问题。 如果这是一个微不足道的问题,请提前道歉。
I am trying to create a new thread by following this tutorial http://www.cplusplus.com/reference/thread/thread/
And following are the relevant code I wrote
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 */The error I get during compiling says
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)I am not very familiar with C++ and templates in general, and I really do not understand what is wrong with my code. Apologize in advance if this would be a trivial question.
最满意答案
你要:
std::thread gameThread(&Game::tick, this); // ^^^ ^^^^You want:
std::thread gameThread(&Game::tick, this); // ^^^ ^^^^更多推荐
发布评论