从命令行编译时"error: expected expression",但不从 Xcode 编译时

"error: expected expression" when compiling from command line but not from Xcode

本文关键字:编译 Xcode 命令行 error expected expression      更新时间:2023-10-16

我分别用clang++(700.1.76)和Xcode 7.1编译了以下代码片段。

#include <iostream>
#include <string>
using namespace std;
class Point {
private:
    int x;
    int y;
public:
    Point(int x1 = 0, int y1 = 0) {
        x = x1;
        y = y1;
    }
    string display() {
        return "(" + to_string(x) + ", " + to_string(y) + ")";
    }
};
class Shape {
private:
    Point bottomLeft;
    Point upperRight;
public:
    Shape(Point bottomLeft1, Point upperRight1) {
        bottomLeft = bottomLeft1;
        upperRight = upperRight1;
    }
    Point getBottomLeft() {
        return bottomLeft;
    }
};
int main(int argc, char const *argv[]) {
    Point p1(1, 2);
    Point p2(3, 4);
    Shape s1(p1, p2);
    Shape s2({1, 2}, {3, 4});
    cout << s1.getBottomLeft().display() << endl;
    cout << s2.getBottomLeft().display() << endl;
    return 0;
}

在Xcode中,我得到的预期输出

(2, 1)
(2, 1)

但是使用clang++,程序无法编译并抛出以下错误:

test.cpp:38:11: error: expected expression
    Shape s2({1, 2}, {3, 4});
             ^

(对于{3, 4},同样的错误也会重复出现。)

这是怎么回事?

在调用clang++时,我需要指定语言标准。

显然,任何高于c++11的东西都可以。