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

时间:2019-03-27 03:20:22

标签: c++

我刚开始使用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.

有人帮助我!谢谢!

1 个答案:

答案 0 :(得分:0)

您的代码正确,因此这似乎与xcode有关。

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