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?
-std=c++11
to your compilation options – Turgid