2015-10-06 86 views
2

我是新來編程和自學C++與Bjarne的書,C++ 11版本。我將Coderunner 2與OS X El Cap上安裝的Xcode命令行工具結合使用。使用初始值設定項列表創建變量時,出現以下代碼錯誤。我的信念是Coderunner沒有運行C++ 11。我是一個完全新手,我不知道該怎麼做我的生活。有用的建議表示讚賞。先謝謝你。Coderunner 2 - 初始化列表錯誤 - C++ 11

鐺版本:蘋果LLVM版本7.0.0(鐺-700.0.72)

#include <iostream> 
    #include <complex> 
    #include <vector> 
    using namespace std; 

    int main(int argc, char** argv) 
    { 
     double d1 = 2.3; //Expressing initialization using = 
     double d2{2.3}; //Expressing initialization using curly-brace-delimited lists 

     complex<double> z = 1; 
     complex<double> z2{d1,d2}; 
     complex<double> z3 = {1,2}; 

     vector<int> v{1,2,3,4,5,6}; 

     return 0; 
    } 

我得到以下錯誤:

2.2.2.2.cpp:9:11: error: expected ';' at end of declaration 
    double d2{2.3}; //Expressing initialization using curly-brace-delimited lists 
      ^
      ; 
    2.2.2.2.cpp:12:20: error: expected ';' at end of declaration 
    complex<double> z2{d1,d2}; 
        ^
         ; 
    2.2.2.2.cpp:13:18: error: non-aggregate type 'complex<double>' cannot be initialized with an initializer list 
    complex<double> z3 = {1,2}; 
        ^ ~~~~~ 
    2.2.2.2.cpp:15:15: error: expected ';' at end of declaration 
    vector<int> v{1,2,3,4,5,6}; 
       ^
       ; 
    4 errors generated. 

回答

1

我可以證實,這個問題是你的編譯器不使用C++ 11模式。當編譯鏗鏘3.4.1 你的代碼,而-std=c++11我得到完全相同的4個錯誤,你,但這個命令行:

c++ -stc=c++11 -c -Wall -pedantic foo.cpp 

只給出了這樣的警告:

warning: unused variable 'z' [-Wunused-variable]
complex z = 1;

+0

謝謝!我想到了。 – rcapac

3

C++ 11不是默認值。使用鐺++,以下是必要編譯在C++ 11:

-std=c++11 -stdlib=libc++ 

在Coderunner 2,則需要修改,通過包括上述涉及C++的腳本。轉到Coderunner>首選項,然後選擇語言C++,然後單擊「編輯腳本」:

Coderunner - Preferences

您將看到Coderunner的「compile.sh」文件。修改行78:

xcrun clang++ -x c++ -std=c++11 -stdlib=libc++ -lc++ -o "$out" "$ 

修改行85:

"$CR_DEVELOPER_DIR/bin/clang" -x c++ -std=c++11 -stdlib=libc++ -lc++ -o "$out" "${files[@]}" "-I$CR_DEVELOPER_DIR/include" "-I$CR_DEVELOPER_DIR/lib/clang/6.0/include" "-I$CR_DEVELOPER_DIR/include/c++/v1" "${@:1}" 

希望幫助!謝謝Serge Ballesta指引我朝着正確的方向前進。