vendredi 1 juillet 2016

Error while pushing elements to a vector inside a lambda function

Below lines of code give error:

std::vector<std::string> strVect;
auto pushToVector = [strVect] () {
    strVect.push_back(std::string("Hi"));
};
pushToVector ();

ERROR:

2 overloads have no legal conversion for 'this' pointer

But when I pass strVect by reference in lambda there is no error.

std::vector<std::string> strVect;
auto pushToVector = [&strVect] () {
    strVect.push_back(std::string("Hi"));
};
pushToVector ();

NO ERROR

Please let me know why do we get error in fist scenario?

Aucun commentaire:

Enregistrer un commentaire