vscode中的C ++:错误:没有匹配的构造函数来初始化'std :: thread'

fuyao

我刚开始使用vscode,并且做了一个有关c ++中多线程的教程。下面的代码与教程相同,并且我确定代码可以在Visual Studio 2017中使用。

#include <iostream>
#include <thread>
#include <string>
#include <mutex>
#include <fstream>
using namespace std;
std::mutex mu;
class LogFile{
    std::mutex mu;
    ofstream f;
    public:
        LogFile(){
            f.open("log.txt");
        }
        void p(string msg, int i){
            std::lock_guard<mutex> locker(mu);
            f << msg << i << endl;
        }
};

void function_1(LogFile& log){
    for(int i=0;i>-100;i--)
        log.p("from t1: ", i);
}
int main(){
    LogFile log;
    std::thread t1(function_1, std::ref(log));
    for (int i=0;i<100;i++)
        log.p("from main: ", i);
    t1.join();
    return 0;
}

错误是:

test.cpp:27:17: error: no matching constructor for initialization of 'std::thread'
    std::thread t1(function_1, std::ref(log));
                ^  ~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/thread:408:9: note: candidate constructor template not viable: requires single argument '__f', but 2 arguments were provided
thread::thread(_Fp __f)
        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/thread:289:5: note: candidate constructor not viable: requires 1 argument, but 2 were provided
    thread(const thread&);
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/thread:296:5: note: candidate constructor not viable: requires 0 arguments, but 2 were provided
    thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
    ^
1 error generated.

谁来帮帮我!谢谢!

J·安东尼奥·佩雷斯

您的代码是正确的,因此这似乎是xcode的问题。

  • 您是否在使用最新版本的XCode?
  • 您是否将标准版本设置为C ++ 11?(命令行opnion: -std=c++11
  • XCode是否使用C ++标准库的最新版本?

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在C ++中没有匹配的构造函数来初始化可变参数模板

C ++ / Threading:没有构造函数“ std :: thread :: thread”的实例>匹配参数列表

C ++中的可调用类对象:没有匹配的函数来调用'std :: tuple <T> :: tuple(<括号包围的初始化程序列表>)'

C ++ 11多线程合并排序,错误为“没有构造函数'std :: thread'的实例与参数列表匹配”

std :: thread初始化构造函数给出了编译错误

C ++没有匹配的构造函数的[]初始化

c++ 中的初始化没有匹配的构造函数错误。我的构造函数有什么问题?

C ++错误:没有匹配的构造函数用于初始化

C ++ 11:为什么我会收到错误消息:调用std :: thread :: thread没有匹配函数?

C++ - 构造函数中的 std::vector 初始化

错误:没有用于初始化“std::shared_ptr<uint8_t []>”的匹配构造函数

没有参数的std :: thread构造函数

错误:没有匹配函数调用std :: thread

在没有复制构造函数的对象的成员函数中启动 std::thread

没有构造函数“ std :: thread :: thread”的实例与参数列表匹配

如何在C ++ 11中使用带有std :: thread作为成员的初始化列表

在构造函数初始化列表中初始化std :: vector数组

std :: thread Args ...列表中的函数指针

使用 Root/C++ 时出错:“TTree”的初始化没有匹配的构造函数

是否可以在构造函数的初始化程序列表中初始化std :: unique_ptr的std :: array?

错误C2512:没有合适的默认构造函数-为什么在构造函数中初始化属性?

如何使用std :: array构造函数参数C ++列表初始化const std :: array成员

使用字符串文字初始化构造函数中的std :: array <char,x>成员。GCC错误?

C ++ std :: thread的行为

在没有构造函数的类中初始化变量

没有构造函数的C ++零初始化

std :: thread类与c ++中的std :: this_thread命名空间?

使用删除的函数'std :: thread :: thread(const std :: thread&)'

C ++ 11使用std :: thread运行模板化类函数