c++ - 尝试在类构造函数中初始化 std::thread 时,函数中出现编译器错误 C2064

标签 c++ multithreading c++11 stdthread

<分区>

我正在编写一个使用线程的类,我想尝试新的 C++11 std::thread。我正在使用 Microsoft Visual Studio Pro 2013 v12.0.31101.00 Update 4 进行编译。该更新是从 2014 年 11 月开始的,因此是最新的。我可以将 std::thread 用于简单用途,但在我下面的代码中出现编译器错误。

如果我注释掉线程的初始化,如下所示:

serial(int port) : port_(port) {} //, reader_thread(&serial::reader_thread_func) {}

然后编译就ok了。

我该如何解决这个问题?

这是我的代码。

序列号.hpp:

/* serial port class */
#ifndef SERIAL_
#define SERIAL_

#include <thread>

class serial
{
public:
    serial(int port) : port_(port), reader_thread(&serial::reader_thread_func) {}
    ~serial();
    bool open();
    bool close();

private:
    std::thread reader_thread;
    void reader_thread_func();
    int port_;
};

#endif // SERIAL_

序列号.cpp:

#include <iostream>

#include "serial.hpp"


void serial::reader_thread_func() {
    std::cout << "reader thread running with thread id: " << std::this_thread::get_id() << '\n';
}

serial::~serial() {
    close();
}

bool serial::open() {

    close(); // in case we are already open

    std::cout << "opening port " << port_ << '\n';
    return true;
}

bool serial::close() {
    std::cout << "closing port " << port_ << '\n';
    return false;
}

要驱动的 main.cpp:

#include "serial.hpp"

int main() {
    serial s(3);
    s.open();

    s.close();
}

编译器错误:

1>------ Build started: Project: thread_in_class, Configuration: Debug Win32 ------
1>  serial.cpp
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(1149): error C2064: term does not evaluate to a function taking 0 arguments
1>          class does not define an 'operator()' or a user defined conversion operator to a pointer-to-function or reference-to-function that takes appropriate number of arguments
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(1137) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall serial::* )(void),void,serial,>,>::_Do_call<,>(std::tuple<>,std::_Arg_idx<>)' being compiled
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(1137) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall serial::* )(void),void,serial,>,>::_Do_call<,>(std::tuple<>,std::_Arg_idx<>)' being compiled
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\thr\xthread(195) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall serial::* )(void),void,serial,>,>::operator ()<>(void)' being compiled
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\thr\xthread(195) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall serial::* )(void),void,serial,>,>::operator ()<>(void)' being compiled
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\thr\xthread(192) : while compiling class template member function 'unsigned int std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *)'
1>          with
1>          [
1>              _Target=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall serial::* )(void),void,serial,>,>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\thr\xthread(187) : see reference to function template instantiation 'unsigned int std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *)' being compiled
1>          with
1>          [
1>              _Target=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall serial::* )(void),void,serial,>,>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\thr\xthread(205) : see reference to class template instantiation 'std::_LaunchPad<_Target>' being compiled
1>          with
1>          [
1>              _Target=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall serial::* )(void),void,serial,>,>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\thread(49) : see reference to function template instantiation 'void std::_Launch<std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall serial::* )(void),void,serial,>,>>(_Thrd_t *,_Target &&)' being compiled
1>          with
1>          [
1>              _Target=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall serial::* )(void),void,serial,>,>
1>          ]
1>          c:\users\angus\documents\visual studio 2013\projects\thread_in_class\thread_in_class\serial.hpp(10) : see reference to function template instantiation 'std::thread::thread<void(__thiscall serial::* )(void),>(_Fn &&)' being compiled
1>          with
1>          [
1>              _Fn=void (__thiscall serial::* )(void)
1>          ]
1>  main.cpp
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(1149): error C2064: term does not evaluate to a function taking 0 arguments
1>          class does not define an 'operator()' or a user defined conversion operator to a pointer-to-function or reference-to-function that takes appropriate number of arguments
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(1137) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall serial::* )(void),void,serial,>,>::_Do_call<,>(std::tuple<>,std::_Arg_idx<>)' being compiled
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(1137) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall serial::* )(void),void,serial,>,>::_Do_call<,>(std::tuple<>,std::_Arg_idx<>)' being compiled
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\thr\xthread(195) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall serial::* )(void),void,serial,>,>::operator ()<>(void)' being compiled
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\thr\xthread(195) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall serial::* )(void),void,serial,>,>::operator ()<>(void)' being compiled
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\thr\xthread(192) : while compiling class template member function 'unsigned int std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *)'
1>          with
1>          [
1>              _Target=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall serial::* )(void),void,serial,>,>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\thr\xthread(187) : see reference to function template instantiation 'unsigned int std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *)' being compiled
1>          with
1>          [
1>              _Target=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall serial::* )(void),void,serial,>,>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\thr\xthread(205) : see reference to class template instantiation 'std::_LaunchPad<_Target>' being compiled
1>          with
1>          [
1>              _Target=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall serial::* )(void),void,serial,>,>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\thread(49) : see reference to function template instantiation 'void std::_Launch<std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall serial::* )(void),void,serial,>,>>(_Thrd_t *,_Target &&)' being compiled
1>          with
1>          [
1>              _Target=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall serial::* )(void),void,serial,>,>
1>          ]
1>          c:\users\angus\documents\visual studio 2013\projects\thread_in_class\thread_in_class\serial.hpp(10) : see reference to function template instantiation 'std::thread::thread<void(__thiscall serial::* )(void),>(_Fn &&)' being compiled
1>          with
1>          [
1>              _Fn=void (__thiscall serial::* )(void)
1>          ]
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

最佳答案

reader_thread_func 是一个类成员函数。它不能作为独立函数调用,只能在类 serial 的实例上调用。您需要将 serial 的实例作为第一个参数提供给 thread:

reader_thread = std::thread(&serial::reader_thread_func, this);
//                                                       ↑↑↑↑

这就是错误“term does not evaluate to a function taking 0 arguments”的意思 - 在 INVOKE 的上下文中,reader_thread_func 接受一个参数:a 串行

关于c++ - 尝试在类构造函数中初始化 std::thread 时,函数中出现编译器错误 C2064,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30005311/

相关文章:

c++ - 未使用的类成员会使代码自行损坏吗?

c++ - 错误 : expected primary-expression before ';' token

c++ - 可变数组 C++

c++运算符关联性/字符串连接/移动语义

c++ - "if"c++ 中的语句不从左到右计算条件

c++ - 从 WinHttp 中的响应设置 Cookie

c++ - 创建类的对象时函数调用的顺序

c++ - 在 SWIG in 和 freearg 类型映射之间传递信息

c++ - 根据硬件并发度将任务均分给线程

linux - rw_semaphore 阻塞 I/O 重线程