错误: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
Error: Range-based 'for' loops are not allowed in C++98 mode
提问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++11
flag 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++11
didn'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_executables
or 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.cpp
works. 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.cpp
and all worked perfectly.
我使用过
g++ -std=c++0x -o program program.cpp
并且一切正常。
回答by BILAL ASLAM
回答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]
回答by subrat2014
If you are using QT5.5
, you can achieve it by adding following lines in your .pro
file.
如果您正在使用QT5.5
,则可以通过在.pro
文件中添加以下几行来实现。
CONFIG += c++11
CONFIG += c++11