簡體   English   中英

從命令行而不是從Xcode編譯時出現“錯誤:預期表達式”

[英]“error: expected expression” when compiling from command line but not from Xcode

我分別使用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都可以。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM