c ++ 11:为什么我重新发现错误:没有匹配函数来调用std :: thread :: thread?

时间:2015-07-27 09:19:16

标签: c++ multithreading c++11

我为实现多线程编写了这个函数,我发现了这个错误,我不明白为什么以及如何修复它。 我正在用这段代码测试c ++ 11线程,但是在创建线程时,我遇到错误没有匹配函数来调用'std :: thread :: thread()'。 我认为问题在于我如何在线程中调用函数,但我看不出有什么问题,对我来说看起来不错。

    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>

1 个答案:

答案 0 :(得分:0)

一个主要问题是您要用作线程函数的函数是成员函数,这意味着您需要Data类的对象实例来调用它们,示例this

cThreads[0] = std::thread( &Data::writeFile, this, cVectorCopy, "outputfile" );
//                                           ^^^^
//                                 Note the `this` here