Linux中编译错误之——‘nullptr’ was not declared in this scope‘

本文介绍了在C++编程中遇到的‘nullptr’未声明的问题,该问题出现在旧版本的g++编译器上。通过添加编译选项-std=gnu++0x或-std=c++11可以解决此问题。编译时务必注意这些选项的使用,特别是在编写makefile时,以避免编译错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++编译.cpp文件,出现错误:

[sxl@VM-4-5-centos string]$ g++ -o mystring string.cpp
In file included from string.cpp:1:0:
string.h: In destructor ‘sxl::string::~string()’:
string.h:48:16: error: ‘nullptr’ was not declared in this scope
         _str = nullptr;

解决方法一:编译时加上-std=gnu++0x

[sxl@VM-4-5-centos string]$ g++ -std=gnu++0x string.cpp -o mystring
[sxl@VM-4-5-centos string]$ ./mystring
hello world
hello world
hello sxl
hello sxl

解决方法二:编译时加上-std=c++11

[sxl@VM-4-5-centos string]$ g++ -std=c++11 string.cpp -o mystring
[sxl@VM-4-5-centos string]$ ./mystring
hello world
hello world
hello sxl
hello sxl

原因:g++版本不一样,有些不支持nullptr,需要利用-std=来设置
具体设置可查看GCC 编译标准-std=的设置方法

注意:在编写makefile文件的时候也是这样写,一定要注意!!!
(我自己就犯了这个错误,试了好多次)

g++ -std=c++11 string.cpp -o mystring  #mystring一定在-o之后,不然会造成不存在mystring文件,会报错
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值