首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c++ 11:为什么我增加了错误:没有调用std::线程::线程的匹配函数?

c++ 11:为什么我增加了错误:没有调用std::线程::线程的匹配函数?
EN

Stack Overflow用户
提问于 2015-07-27 09:19:16
回答 1查看 1.5K关注 0票数 1

我编写了这个函数来实现multithrading,我增加了这个错误,我不明白为什么以及如何修复它。我正在用这段代码测试c++11线程,但是在创建线程时,对‘std::c++11::线程()’的调用没有匹配函数的错误。我认为问题在于我如何调用线程中的函数,但我看不出有什么问题,在我看来很好。

代码语言:javascript
复制
    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();
        }

调用的函数

代码语言:javascript
复制
    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

    }

现在输出:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-27 09:24:39

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

代码语言:javascript
复制
cThreads[0] = std::thread( &Data::writeFile, this, cVectorCopy, "outputfile" );
//                                           ^^^^
//                                 Note the `this` here
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31649282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档