学习C++ Primer这本书,在敲练习1.20的时候出现这样的错误
In file included from test.cpp:2:0:
Sales_item.h:56:20: warning: defaulted and deleted functions only available with -std=c++11 or -std=gnu++11
Sales_item() = default;
^
Sales_item.h:70:27: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
unsigned units_sold = 0; // explicitly initialized
^
Sales_item.h:71:22: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
double revenue = 0.0;
^
Sales_item.h:45:7: error: constructor required before non-static data member for ‘Sales_item::units_sold’ has been parsed
class Sales_item {
^
Sales_item.h:45:7: error: constructor required before non-static data member for ‘Sales_item::revenue’ has been parsed
在网上搜了一下,好像是因为编译器不支持C++11标准
解决办法:在编译时加上 -std=c++11
即编译时输入命令
g++ -o test test.cpp -std=c++11
除了g++ , gcc 也可以类似方法支持C11
编译选项为 -std=c11