错误:C++98 模式下不允许基于范围的“for”循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22859183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:14:55  来源:igfitidea点击:

Error: Range-based 'for' loops are not allowed in C++98 mode

c++loopsfor-loopcodeblocks

提问by Sindre

So I'm following the tutorials on this page: http://www.cplusplus.com/doc/tutorial/control/But I'm having trouble doing a range/based for loop. I found this page: GNU GCC compiler updatingThe answer there says I should open "Project" and "Properties". But when I try that, the "Properties" option is grayed out with no explanation: http://imageshack.com/a/img571/4371/xd1x.pngSo.. how can I activate range/based for loops?

所以我正在关注此页面上的教程:http: //www.cplusplus.com/doc/tutorial/control/但我在执行范围/基于 for 循环时遇到问题。我找到了这个页面: GNU GCC compiler updates那里的答案说我应该打开“项目”和“属性”。但是当我尝试这样做时,“属性”选项变灰,没有任何解释:http: //imageshack.com/a/img571/4371/xd1x.png那么..如何激活范围/基于循环?

回答by user3159253

Pass -std=c++11flag to the compiler. Certainly GCC should be fresh enough (>=4.7) to support all these modern standards. For CodeBlocks 13.12: Settings -> Compiler -> Tab "Compiler Flags" -> Option "Have g++ follow the C++11 ISO C++ [-std=c++11]"

-std=c++11标志传递给编译器。当然,GCC 应该足够新鲜(>=4.7)以支持所有这些现代标准。对于 CodeBlocks 13.12:设置 -> 编译器 -> 选项卡“编译器标志”-> 选项“让 g++ 遵循 C++11 ISO C++ [-std=c++11]”

回答by mythicalcoder

The above given solution of using -std=c++11didn't work for me.

上面给出的使用解决方案-std=c++11对我不起作用。

This is the target and version detail of my compiler.
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) Target: x86_64-linux-gnu

这是我的编译器的目标和版本详细信息。
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) Target: x86_64-linux-gnu

When I tried, this is what happened.
$ g++ -std=c++11 program.cpp cc1plus: error: unrecognized command line option ‘-std=c++11'

当我尝试时,这就是发生的事情。
$ g++ -std=c++11 program.cpp cc1plus: error: unrecognized command line option ‘-std=c++11'

This solved the problem for me.
$ g++ -std=c++0x program.cpp

这为我解决了这个问题。
$ g++ -std=c++0x program.cpp

回答by Akash Chavan

Both of these:

这两个:

g++ -std=c++11 -o test_executable test_source.cpp
g++ -std=c++0x -o program program.cpp

worked for me.

为我工作。

Only thing to do after compiling is to execute the test_executable(in the first case) as ./test_executablesor program (in the second case) as ./program.

编译后唯一要做的就是执行test_executable(在第一种情况下) as./test_executables或程序(在第二种情况下) as ./program

回答by CTguest

Using the above solution g++ -std=c++0x program.cppworks. However, the command needs to be modified slightly in order to run the program with the common command: ./program

使用上述解决方案 g++ -std=c++0x program.cpp有效。但是,为了使用通用命令运行程序,需要稍微修改该命令: ./program

I used g++ -std=c++0x -o program program.cppand all worked perfectly.

我使用过 g++ -std=c++0x -o program program.cpp并且一切正常。

回答by BILAL ASLAM

Dev-Cpp 5.11 In simple you can click on Tool>Compiler Option>Setting>Code Generation>and last option language std selsected(ISO C++ 11).

Dev-Cpp 5.11 简单来说,您可以单击工具>编译器选项>设置>代码生成>和最后一个选项语言标准选择(ISO C++ 11)。

enter image description here

在此处输入图片说明

回答by Tridib Samanta

If you faced this error in Code::Blocks, this might help you -

如果您在Code::Blocks 中遇到此错误,这可能会对您有所帮助 -

Click on Settings -> Compiler -> Compiler Settings -> Compiler Flags

点击设置 -> 编译器 -> 编译器设置 -> 编译器标志

Under the "General" section, check [?] the box beside : Have g++ follow the C++11 ISO C++ language standard [-std=c++11]

在“General”部分下,选中 [?] 旁边的框: Have g++ follow the C++11 ISO C++ language standard [-std=c++11]

Check image for reference

检查图像以供参考

回答by subrat2014

If you are using QT5.5, you can achieve it by adding following lines in your .profile.

如果您正在使用QT5.5,则可以通过在.pro文件中添加以下几行来实现。

CONFIG += c++11

CONFIG += c++11