Why am I getting a warning for this range-based for loop in C++?
Asked Answered
D

5

19

I am currently self-teaching myself C++ using Bjarne Stroustrup's book (2nd ed). In one of the examples, he uses a range-for-loop to read the elements in a vector. When I wrote and compiled the code for myself I get this warning. When I run the code, it seems to be working and calculates the mean. Why am I receiving this warning and should I ignore it? Also, why is the range-for using int instead of double in the example, but still returns a double?

temp_vector.cpp:17:13: warning: range-based for loop is a C++11 
extension [-Wc++11-extensions]

This is the code

#include<iostream>
#include<vector>

using namespace std;

int main ()
{
  vector<double> temps;     //initialize a vector of type double

  /*this for loop initializes a double type vairable and will read all 
    doubles until a non-numerical input is detected (cin>>temp)==false */
  for(double temp; cin >> temp;)
    temps.push_back(temp);

  //compute sum of all objects in vector temps
  double sum = 0;

 //range-for-loop: for all ints in vector temps. 
  for(int x : temps)     
    sum += x;

  //compute and print the mean of the elements in the vector
      cout << "Mean temperature: " << sum / temps.size() << endl;

  return 0;
}

On a similar note: how should I view the range-for in terms of a standard for loop?

Denizen answered 14/3, 2018 at 4:14 Comment(1)
Add -std=c++11 to your compilation optionsTurgid
G
27

Since nobody is showing how to use C++ 11 with g++, it looks like this...

g++ -std=c++11 your_file.cpp -o your_program

Hopefully this saves Google visitors an extra search.

Grippe answered 21/1, 2019 at 21:45 Comment(2)
I did end up figuring out how to do this eventually but it's exactly what I needed at the time. I hope it does as well. Thank you!Autotype
Is there a more permanent way to fix this instead of always having to type this out ? I am using g++ with Apple clang 14Disarray
H
21

Pass --std=c++11 to the compiler; your (ancient) compiler is defaulting to C++03 and warning you that it is accepting some newer C++ constructs as extensions.


Ranged base for is expanded into an iterator-based for loop, but with less opportunities for typos.

Hazardous answered 14/3, 2018 at 4:18 Comment(5)
How do I do that? I'm using g++ on mac through the terminal.Autotype
As said: pass "--std=c++11" to the compiler options.Accidence
Is there a way to always compile with c++11/14 by default using g++ command?Treva
@AbhijeetKhangarot That looks like a good question to ask on stack overflow. Press the [ask question] button to the top right of this web page (location may vary on mobile). There are too many subtle details to answer it in a comment, really (aliases, updates, shell scripts among other possibilities). I am not an expert on the subject, others will be.Hazardous
Asked here, if you're interested in answering :) #68985958Treva
K
9

For those who use code runner extension on VS code, go to code runner extension settings. find -> Code-runner:Executer map. Click on edit in settings.json and find cpp script for terminal and set it like follows:

"cpp": "cd $dir && g++  -std=c++11 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
Kinsfolk answered 16/9, 2021 at 9:57 Comment(2)
works as of Jan 2023 in VScode on Code-RunnerCaustic
In the version of VSCode current as of 10 May 23, the setting is in tasks.json, as: "tasks": [ { "type": "cppbuild", "label": "C/C++: clang++ build active file", "command": "/usr/bin/clang++", "args": [ "-fcolor-diagnostics", "-fansi-escape-codes", "-std=c++17", "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ],Megdal
B
1

This is because you are using for (int x: temps) which is a c++11 construct. Try the following if you are using eclipse:

  • Right-click on the the project and select "Properties"

  • Navigate to C/C++ Build -> Settings

  • Select the Tool Settings tab.

  • Navigate to GCC C++ Compiler -> Miscellaneous

  • In the option setting labeled Other Flags add -std=c++11

Now rebuild your project.

Update: For Atom follow below steps:

Go to ~/.atom/packages/script/lib/grammers.coffee

Go to the C++ section (ctrl-f c++):

Then change this line:

args: (context) -> ['-c', "xcrun clang++ -fcolor-diagnostics -Wc++11-extensions // other stuff

to this:

args: (context) -> ['-c', "xcrun clang++ -fcolor-diagnostics -std=c++11 -stdlib=libc++ // other stuff

i.e. add -std=c++11 -stdlib=libc++ and remove -Wc++11-extensions

Hope this helps!

Belding answered 14/3, 2018 at 4:21 Comment(3)
More recent versions of Eclipse have a Dialect property page under C++ Compiler that allows you to set this more easilyHomosporous
I'm not using Eclipse, I'm using the Atom text editor and the mac terminal.Autotype
@EfraínGonzález: Added instructions for AtomBelding
A
0

For clang users: use clang++ -std=c++11 test.cpp

Aloha answered 23/9, 2023 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.