我编写了这个函数来实现multithrading,我增加了这个错误,我不明白为什么以及如何修复它。我正在用这段代码测试c++11线程,但是在创建线程时,对‘std::c++11::线程()’的调用没有匹配函数的错误。我认为问题在于我如何调用线程中的函数,但我看不出有什么问题,在我看来很好。
void Data::Set( const BeBoard* pBoard, const std::vector<uint32_t>& pData, uint32_t pNevents, bool swapBytes )
{
std::vector<uint32_t> cVectorCopy;
cVectorCopy = deepCopy( pData );
std::thread cThreads[2];
for ( int i = 0; i < 2; ++i )
{
cThreads[0] = std::thread( Data::writeFile, cVectorCopy, "outputfile" );
cThreads[1] = std::thread( Data::SetSpecialized, pBoard, pData, pNevents, swapBytes );
for ( int i = 0; i < 2; ++i )
cThreads[i].join();
}调用的函数
void Data::writeFile( std::vector<uint32_t> fData , std::string fFileName )
{
//check if the file already exists
if ( boost::filesystem::exists( fFileName + ".bin" ) )
remove( ( fFileName + ".bin" ).c_str() );
std::ofstream ofile( ( fFileName + ".bin" ).c_str(), std::ios::binary );
//write the bin file
for ( auto& cElements : fData )
ofile.write( ( char* ) &cElements, sizeof( uint8_t ) );
}
void Data::SetSpecialized( const BeBoard* pBoard, const std::vector<uint32_t>& pData, uint32_t pNevents, bool swapBytes )
{
// just do a swap of the bytes
}现在输出:
<pre><code>
Data.cc: In member function ‘void Ph2_HwInterface::Data::Set(const Ph2_HwDescription::BeBoard*, const std::vector<unsigned int>&, uint32_t, bool)’:
Data.cc:38:74: error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, std::vector<unsigned int>&, const char [11])’
cThreads[0] = std::thread( Data::writeFile, cVectorCopy, "outputfile" );
Data.cc:38:74: note: candidates are:
In file included from ../Utils/Data.h:22:0,
from Data.cc:12:
/opt/rh/devtoolset-2/root/usr/include/c++/4.8.2/thread:133:7: note: std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (Ph2_HwInterface::Data::*)(std::vector<unsigned int>, std::basic_string<char>); _Args = {std::vector<unsigned int, std::allocator<unsigned int> >&, const char (&)[11]}]
thread(_Callable&& __f, _Args&&... __args)
^
/opt/rh/devtoolset-2/root/usr/include/c++/4.8.2/thread:133:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘void (Ph2_HwInterface::Data::*&&)(std::vector<unsigned int>, std::basic_string<char>)’
/opt/rh/devtoolset-2/root/usr/include/c++/4.8.2/thread:128:5: note: std::thread::thread(std::thread&&)
thread(thread&& __t) noexcept
^
/opt/rh/devtoolset-2/root/usr/include/c++/4.8.2/thread:128:5: note: candidate expects 1 argument, 3 provided
/opt/rh/devtoolset-2/root/usr/include/c++/4.8.2/thread:122:5: note: std::thread::thread()
thread() noexcept = default;
^
/opt/rh/devtoolset-2/root/usr/include/c++/4.8.2/thread:122:5: note: candidate expects 0 arguments, 3 provided
Data.cc:40:88: error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, const Ph2_HwDescription::BeBoard*&, const std::vector<unsigned int>&, uint32_t&, bool&)’
cThreads[1] = std::thread( Data::SetSpecialized, pBoard, pData, pNevents, swapBytes );
</pre></code>发布于 2015-07-27 09:24:39
一个主要的问题是,您想要用作线程函数的函数是成员函数,这意味着您需要Data类的对象实例来调用它们,例如this。
cThreads[0] = std::thread( &Data::writeFile, this, cVectorCopy, "outputfile" );
// ^^^^
// Note the `this` herehttps://stackoverflow.com/questions/31649282
复制相似问题