我目前正在阅读Bjarne Stroustrup的“C ++编程语言(第4版)”,并试图了解以下语法错误或这是我编译它的方式。
根据以下代码片段(摘自本书),我的构造函数应如下所示:
class Vector {
public:
Vector(int s): elem{new double[s]}, sz{s} {} // Construct a Vector
...
private:
double* elem; // Pointer to the elements
int sz; // The number of elements
};
但是,我无法编译它,除非根据错误消息的建议更改以下内容:
Vector(int s): elem(new double[s]), sz(s) {}
注意:我通过Mac终端使用以下命令编译了我的C ++代码:
g++ -ansi -pedantic -Wall Test.cpp -o Test.o
提前谢谢。
答案 0 :(得分:2)
可能你需要做的就是为C ++ 11编译它:
g++ -std=c++11 -ansi -pedantic -Wall Test.cpp -o Test.o
正如@molbdnilo在评论中指出的那样,从命令中删除-ansi
,因为这是std=c89
或std=c++98
的同义词。
g++ -std=c++11 -pedantic -Wall Test.cpp -o Test.o