gpt4 book ai didi

multithreading - 由于模板和线程问题,c++ 11编译失败

转载 作者:行者123 更新时间:2023-12-03 13:17:00 25 4
gpt4 key购买 nike

我正在尝试按照本教程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++和模板不是很熟悉,我真的不明白我的代码有什么问题。如果这是一个琐碎的问题,请提前道歉。

最佳答案

你要:

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

关于multithreading - 由于模板和线程问题,c++ 11编译失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46820763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com