试图使用已删除的函数

Thread (Attempt to use a deleted function

本文关键字:删除 函数      更新时间:2023-10-16

我正在跟随一个关于线程的在线教程,并得到了错误消息"语义问题:试图使用已删除的函数"。知道怎么了吗?

#include <iostream>
#include <thread> 
#include <string>
using namespace std;
class Fctor {
public:
    void operator() (string & msg) {
        cout << "t1 says: " << msg << endl;
        msg = "msg updated";
    }
};

int main(int argc, const char * argv[]) {
    string s = "testing string " ;
    thread t1( (Fctor()), s);
    t1.join();
    return 0;
}

好吧,代码与VS2015, MS-Compiler一起工作,对代码进行以下更改:

void operator() (string & msg) {
    cout << "t1 says: " << msg << endl;
    msg = "msg updated";
}

void operator() (std::string& msg) {
    std::cout << "t1 says: " << msg.c_str() << std::endl;
    msg = "msg updated";
}

string s = "testing string " ;
thread t1( (Fctor()), s);

std::string s = "testing string ";
Fctor f;
std::thread t1(f, s);

我更改的两个主要内容是msg.c_str(),因为流不接受string,而是接受const char*。其次,我将RValue因子()转换为LValue因子f并将f作为参数,线程显然不接受RValue。