86011

Range For loop help: Expected initializer before “:” token

I am completely new to C++ and I am now following the C++ Primer book.

I wrote a small example about strings, here is the code:

#include <iostream> #include <string> #include <cctype> using std::cin; using std::cout; using std::endl; using std::string; int main() { string s("Hello World"); for (auto &c : s) c = toupper(c); cout << s << endl; return 0; }

I am on Linux with GCC version 4.4.6 and I tried to compile this code with:

g++ test_strings.c -std=c++0x

but got the following errors:

test_strings.c: In function 'int main()': test_strings.c:14: error: expected initializer before ':' token test_strings.c:19: error: expected primary-expression before 'return' test_strings.c:19: error: expected ')' before 'return'

I copied the program from the textbook, so I though it was a misspelling but after a check and trying searching on the web and updating my gcc the error reminds. Help will be greatly appreciated, thanks in advance.

Answer1:

As per the C++0x/C++11 Support in GCC page, you need to be running gcc 4.6 to get the range-for feature.

The 4.6 changes page contains:

Improved experimental support for the upcoming C++0x ISO C++ standard, including support for constexpr (thanks to Gabriel Dos Reis and Jason Merrill), nullptr (thanks to Magnus Fromreide), noexcept, unrestricted unions, <strong>range-based for loops</strong> (thanks to Rodrigo Rivas Costa), opaque enum declarations (thanks also to Rodrigo), implicitly deleted functions and implicit move constructors.

Since you're running gcc 4.4.6, it's not available to you.

Recommend

  • WebServices on Unity, Dll, compatibility with .Net 4.0
  • Multi-parameter type synonym instances
  • Different between rotating the camera vs rotating the scene point (only the point, not the entire sc
  • Import object class definitions to Active Directory (AD LDS)
  • segfault when using boost::signal with -D_GLIBCXX_DEBUG compiler flag
  • How to add shards dynamically to collection in solr?
  • How can I write a file to a folder of the internal storage on Android?
  • 'docker ps' hangs forever after server restart
  • why '\\97' ascii value equals 55
  • How to use Xcode Extract refactoring feature?
  • Cannot establish BLE connection between Android 5 and BLE device
  • Is Android's ARGB_8888 Bitmap internal format always RGBA?
  • Is C++ compilable with OpenMP and boost on MacOS?
  • How do I compile a C/C++ program through windows command prompt?
  • Portable JRE on Linux - possible?
  • configure: error: no acceptable C compiler found in $PATH
  • NUnit 3.0 TestCase const custom object arguments
  • How to revert to previous XCode version?
  • Groovy: Unexpected token “:”
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Read text file and split every line in MSBuild
  • Where to put my custom functions in Wordpress?
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Numpy divide by zero. Why?
  • Linker errors when using intrinsic function via function pointer
  • Hits per day in Google Big Query
  • coudnt use logback because of log4j
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Qt: Run a script BEFORE make
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • Net Present Value in Excel for Grouped Recurring CF
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?