在一个简单的代码演示中使用 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&,0>(_Fn,int &)' being compiled
        with
        [
            _Fn=ts &
        ]

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)