2015-08-23 183 views
3

我下面就穿一个在线教程,并得到了错误消息“的语义问题:尝试使用删除功能”?任何想法有什么错主题(尝试使用删除功能

#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; 
} 
+0

什么是完整错误? – 0x499602D2

+0

由Fctor()构造的对象的活动时间不够长,无法在线程中使用。 – bmargulies

+2

@bmargulies不,这很好。这是另一个参数's'的问题。我猜'std :: ref(s)'会起作用。 – juanchopanza

回答

0

好中,代码与VS2015,MS-编译器,与这些改变到代码:

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(),因为该流不走的字符串,但为const char *。其次,我将RValue Fctor()转换为LValue Fctor f并给出f作为参数,线程显然不需要RValues。