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文件,会报错