Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Lambda -- String params not working in following function calls

Tags:

c++

lambda

I'm having an issue where when I try to access a string that's the main param in a lambda, the compiler doesn't recognize it as so when I try to call functions using that string.

Here is my code:

void removePunctuation(std::vector<std::string> &inTokens,
                   std::vector<std::string> &outTokens) {
std::for_each(inTokens.begin(), inTokens.end(), [outTokens](std::string s) {
    std::string newS = s;
    // newS.erase(std::remove_if(newS.begin(), newS.end(), ispunct));
    outTokens.push_back(newS);});
}

And the following error is produced:

a2.cpp:114:19: error: no matching member function for call to 'push_back'
    outTokens.push_back(newS);});

I am also getting this sort of error in other functions, when I try to call a function that uses the lambda's string param in its call.

Any help is much appreciated!


1 Answers

By default, lambda arguments are passed as read-only, try

[&outTokens](std::string s)

(It is perhaps what you want anyway, if the outTokens parameter is expected to be modified.)

like image 55
AlexD Avatar answered Sep 28 '26 05:09

AlexD