尝试在类构造函数中初始化 std::thread 时出现编译器错误 C2064 [重复]

Posted

技术标签:

【中文标题】尝试在类构造函数中初始化 std::thread 时出现编译器错误 C2064 [重复]【英文标题】:Compiler error C2064 in functional when try to initialise a std::thread in a class constructor [duplicate] 【发布时间】:2015-05-02 17:26:32 【问题描述】:

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

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

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

然后就可以编译了。

我该如何解决这个问题?

这是我的代码。

serial.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_

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 ==========

【问题讨论】:

【参考方案1】:

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

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

这就是错误“术语不计算为采用 0 个参数的函数”的含义 - 在 INVOKE 的上下文中,reader_thread_func 采用一个参数:serial

【讨论】:

以上是关于尝试在类构造函数中初始化 std::thread 时出现编译器错误 C2064 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

带有类参数的 std::thread 初始化导致类对象被多次复制

std::thread,类构造函数和析构函数

std::thread 通过引用传递调用复制构造函数

std::thread 按引用传递调用复制构造函数

c++11的std::thread能用类的成员函数构造一个线程吗?语法怎样?

是否可以定义一个 std::thread 并稍后对其进行初始化?