2017-04-23 71 views
1

Mac上的鐺聲不支持統一初始化嗎?Mac上的鐺聲不支持統一初始化嗎?

我試過編譯下面的代碼,但是編譯器會報錯。

#include <iostream> 
#include <vector> 
#include <string> 

int main() { 
    std::vector<int> v = {3, 1, 9, 4}; 
    std::cout << v[1] << std::endl; 
} 

錯誤:

vector.cpp:9:22: error: non-aggregate type 'std::vector<int>' cannot be initialized with an initializer list 
    std::vector<int> v = {3, 1, 9, 4}; 
        ^ ~~~~~~~~~~~~ 
1 error generated. 

OS:MacOS的10.12.4

編譯器版本:

Apple LLVM version 8.1.0 (clang-802.0.42) 
Target: x86_64-apple-darwin16.5.0 
Thread model: posix 
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin 
+4

你應該總是在你的問題中包含任何編譯錯誤(逐字)。 –

+1

確保代碼實際上是用C++ 11編譯的,即在你的編譯設置中,檢查「C++語言方言」是否設置爲「C++ 11」,這意味着編譯器標誌「-std = C++ 1「來使用。 –

回答

2

我懷疑你是不是編譯代碼爲C++ 11/14(-std=c++11-std=c++14)?如果不;去做。 Clang 確實支持你正在嘗試做的事情。