首页 > 解决方案 > 在简单代码演示中使用 std::thread 时有关重载函数的 C++ 编译错误

问题描述

我正在使用 std::thread 编写以下演示:

#include <thread>
#include<mutex>
#include<vector>
class ts {
    public:
        ts() {};
        ~ts() {};
        void operator() (int x) {
            mu.lock();
            std::cout << "i'd like to output " << x << ", thanks\n";
            mu.unlock();
        }
        std::mutex mu;
};
int main()
{
    ts t;
    std::vector<std::thread> v;
    for (int i = 0; i < 4; i++)
        v.push_back(std::thread(t,i));
    for (int i = 0; i < 4; i++)
        v[i].join();
    return 0;
}

编译时,我得到一个错误:

memory(3378): error C2661: 'std::tuple<ts,int>::tuple': no overloaded function takes 2 arguments
thread(67): note: see reference to function template instantiation 'std::unique_ptr<_Tuple,std::default_delete<_Tuple>> std::make_unique<_Tuple,ts&,int&,0>(ts &,int &)' being compiled
thread(89): note: see reference to function template instantiation 'void std::thread::_Start<ts&,int&>(_Fn,int &)' being compiled
        with
        [
            _Fn=ts &
        ]
<source>(22): note: see reference to function template instantiation 'std::thread::thread<ts&,int&,0>(_Fn,int &)' being compiled
        with
        [
            _Fn=ts &
        ]

标签: c++multithreadingstdthread

解决方案


推荐阅读