3

Just start learning CS106B , Error messages telling me something wrong with these 3 lines.

" Error : expected body of lambda expression "

string key = aToken.substr([i],1);

myMap.put(aToken.substr([i],1),1);

else {myMap[aToken.substr([i],1)] +=1};

int main() {

TokenScanner myTK;
myTK.setInput("Sven is Pro Moo Noob <naja>");
myTK.ignoreWhitespace();
Map <string ,int> myMap;
while(myTK.hasMoreTokens()){
    string aToken = myTK.nextToken();
    for(int i=0;i<= int(aToken.size());i++){
        string key = aToken.substr([i],1);
        if(!myMap.containsKey(key)){
                myMap.put(aToken.substr([i],1),1);
        }
        else {myMap[aToken.substr([i],1)] +=1};
    }
    cout << aToken << endl;
}
cout<< myMap.toString() << endl;
return 0;
};
zch
  • 14,931
  • 2
  • 41
  • 49
user3029502
  • 43
  • 1
  • 1
  • 5

1 Answers1

2

Lambda expression is expression that usually takes form:

[capture list](parameters) {function body}

When compiler notices your [i] it expects that it's beginning of lambda expression. There is no reason to wrap numbers in square brackets in your case.

Lambda expressions allow in-line construction of functor objects with anonymous class. See: What is a lambda expression in C++11?

Community
  • 1
  • 1
zch
  • 14,931
  • 2
  • 41
  • 49