c++ - 在类中没有使用 C++ 线程的类型名称“类型”
问题描述
我目前在使用 c++ 线程时遇到了问题。我有以下错误:
/usr/include/c++/4.8/thread:140:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (TextMove::*)(); _Args = {}]’
textMove.cc:8:40: required from here
/usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (TextMove::*)()>()>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/usr/include/c++/4.8/functional:1727:9: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (TextMove::*)()>()>’
_M_invoke(_Index_tuple<_Indices...>)
但我的代码似乎正确使用了这些功能:
class TextMove : Text{
public:
TextMove(Viewport *v,rgb_matrix::VFont *f,rgb_matrix::Color *c,rgb_matrix::Color *bg,char* content,int extra) : Text(v,f,0,0,c,bg,content,extra){
index = 0;
limit = v->getX();
}
void play(int speed);
void playThread(int speed);
private:
void draw();
int index;
pthread_t thread;
int limit;
};
void TextMove::play(int speed){
std::thread t(&TextMove::playThread,speed);
t.join();
}
void TextMove::playThread(int speed) {
index = 0;
while(_x>limit){
draw();
sleep(speed);
_x--;
}
}
我不知道是不是因为我在课堂上使用它,但我暂时没有发现问题。
解决方案
正如Smit Shah在评论中指出的那样,您可以:
- 使用以下
std::thread
构造函数
template< class Function, class... Args >
explicit thread( Function&& f, Args&&... args );
为了传递this
论据。
std::thread t(&TextMove::playThread , this , speed);
t.join();
- 或者您可以在构造线程时简单地传递一个 lambda。
std::thread t([this , &speed]{playThread(speed);});
t.join();
推荐阅读
- android - 如何在面向 Android Q 的应用程序中将图像保存到公共外部存储中的单独文件夹中?
- reactjs - 如何使用 React Scroll for React JS 在屏幕上确保页面刷新
- sql - 如何在 SQL 中按某些列而不是其他列进行聚合
- python - 并行使用 Python 的 dask 将多个 CSV 分别读取到无法正常工作的数据帧列表中
- azure - 如何将结构添加到 Azure IoT Central 自定义应用程序
- mysql - 通过合并的 LEFT JOIN 过滤
- c++ - libSDL2-mixer:如何更改 wav 文件 MixChunk 中的样本?
- node.js - 猫鼬 .exec() 不起作用,但承诺可以
- performance - 多处理器缓存一致性
- scripting - 无法弄清楚如何从 Az Devops CLI 创建一个空的构建管道