Eclipse IDE 中的 C++ 错误“未在此范围内声明 nullptr”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10033373/
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-27 13:31:22  来源:igfitidea点击:

C++ Error 'nullptr was not declared in this scope' in Eclipse IDE

c++g++eclipse-cdt

提问by Vivek Ragunathan

I am running Eclipse Helios and I have g++-4.6 installed. Hope I am not wrong that g++4.6 implements C++ 11 features. I have created a C++ project which uses the nullptr and auto keywords. The build gives the following errors:-

我正在运行 Eclipse Helios 并且安装了 g++-4.6。希望我没有错 g++4.6 实现了 C++11 特性。我创建了一个使用 nullptr 和 auto 关键字的 C++ 项目。构建给出了以下错误:-

../{filename}.cpp:13:13: error: ‘nullptr' was not declared in this scope

../{filename}.cpp:14:2: warning: ‘auto' will change meaning in C++0x; please remove it [-Wc++0x-compat]

Actually it was building fine until yesterday. I am getting these from nowhere today. Please help me solve this problem.

事实上,直到昨天它才建造得很好。我今天不知从哪里得到这些。请帮我解决这个问题。

采纳答案by Vivek Ragunathan

Finally found out what to do. Added the -std=c++0x compiler argument under Project Properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Miscellaneous. It works now!

终于知道该怎么做了。在 Project Properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Miscellaneous 下添加 -std=c++0x 编译器参数。它现在有效!

But how to add this flag by default for all C++ projects? Anybody?

但是如何为所有 C++ 项目默认添加这个标志?有人吗?

回答by Rob I

According to the GCC page for C++11:

根据C++11GCC 页面

To enable C++0x support, add the command-line parameter -std=c++0x to your g++ command line. Or, to enable GNU extensions in addition to C++0x extensions, add -std=gnu++0x to your g++ command line. GCC 4.7 and later support -std=c++11 and -std=gnu++11 as well.

要启用 C++0x 支持,请将命令行参数 -std=c++0x 添加到 g++ 命令行。或者,要启用除 C++0x 扩展之外的 GNU 扩展,请将 -std=gnu++0x 添加到您的 g++ 命令行。GCC 4.7 及更高版本也支持 -std=c++11 和 -std=gnu++11。

Did you compile with -std=gnu++0x?

你编译了-std=gnu++0x吗?

回答by The Beast

You are using g++ 4.6 version you must invoke the flag -std=c++0x to compile

您使用的是 g++ 4.6 版本,您必须调用标志 -std=c++0x 进行编译

g++ -std=c++0x *.cpp -o output

g++ -std=c++0x *.cpp -o 输出

回答by josephthomas

Is that an actual compiler error or a Code Analysis error? Some times the code analysis can be a bit sketchy and report non-valid errors.

这是实际的编译器错误还是代码分析错误?有时代码分析可能有点粗略并报告无效错误。

To turn off code analysis for the project, right click on your project in the Project Explorer, click on Properties, then go to the C/C++ General tab, then Code Analysis. Then click on "Use Project Settings" and disable the ones that you do not wish for.

要关闭项目的代码分析,请在项目资源管理器中右键单击您的项目,单击属性,然后转到 C/C++ 常规选项卡,然后转到代码分析。然后单击“使用项目设置”并禁用您不希望的设置。

Also, are you sure you are compiling with the C++11 compiler?

另外,您确定要使用 C++11 编译器进行编译吗?

回答by Leon

Go to Settings -> Compiler... And add flag to "Have g++ follow the coming C++0x ISO C++ language standard [std=c++0x]

转到设置 -> 编译器...并添加标志到“让 g++ 遵循即将到来的 C++0x ISO C++ 语言标准 [std=c++0x]

回答by qie

I add the ",-std=c++0x" after "-c -fmessage-length=0",under Project Properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Miscellaneous. Dont't forget to add the comma "," as the seperator.

我在“-c -fmessage-length=0”之后添加了“,-std=c++0x”,在Project Properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Miscellaneous 下。不要忘记添加逗号“,”作为分隔符。

回答by JobJob

Trying with a different version of gcc worked for me - gcc 4.9 in my case.

尝试使用不同版本的 gcc 对我有用 - 在我的情况下是 gcc 4.9。