简体   繁体   中英

“auto changes meaning in c++11”

#include<iostream>
#include<string>
#include<iterator>
using namespace std;
int main()
{
    string a("hello world");
    for(auto it = a.begin(); it != a.end() && !isspace(*it); it++ )
    {
        *it = toupper(*it);
    }
    cout<<a;
}

There are two errors I get. One is as mentioned, "auto changes meaning in c++11" and the other is "!= operator not defined." Never had this problem before.

I only used the auto operator because the book suggested.

I'm a beginner, and getting back to learning after about 2 months. Having trouble catching up.

Your code runs ok when compiled with -std=c++11 , You may check it here .

You can add the option in Setting->Compiler->Have g++ follow the C++11 ISO C++ language standard [-std=C++11] in CodeBlocks.

As chris mentioned, using Range-based for loop is much better. It's closer to spirit of C++11 and it's easier to learn for beginners. Consider:

    #include <string>
    #include <iostream>
    int main()
    {
       std::string s{"hello, world"}; // uniform initialization syntax is better
       for (auto& c : s)  // remember to use auto&
         if (!isspace(c)) c = toupper(c);
       cout << s << '\n'; // remember the last newline character  
       return 0;
    }

-- Saeed Amrollahi Boyouki

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM