小编Efr*_*lez的帖子

为什么我会在C++中为这个基于范围的循环收到警告?

我目前正在使用Bjarne Stroustrup的书(第2版)自学C++.在其中一个示例中,他使用for-for-loop来读取向量中的元素.当我为自己编写和编译代码时,我得到了这个警告.当我运行代码时,它似乎正在工作并计算平均值.为什么我收到这个警告,我应该忽略它吗?另外,为什么范围 - 在示例中使用int而不是double,但仍返回double?

temp_vector.cpp:17:13: warning: range-based for loop is a C++11 
extension [-Wc++11-extensions]
Run Code Online (Sandbox Code Playgroud)

这是代码

#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 …
Run Code Online (Sandbox Code Playgroud)

c++ warnings for-loop compiler-warnings c++11

5
推荐指数
3
解决办法
3481
查看次数

标签 统计

c++ ×1

c++11 ×1

compiler-warnings ×1

for-loop ×1

warnings ×1