Coderunner 2 -初始化列表错误- c++ 11



我是编程新手,用Bjarne的书自学c++, c++ 11版。我使用Coderunner 2和安装在OS X El Cap上的Xcode命令行工具。当使用初始化器列表创建变量时,我得到以下代码的错误。我认为coderrunner并没有运行c++11。我完全是个新手,我不知道该怎么办才好。有帮助的建议是值得赞赏的。提前谢谢你。

clang version: Apple LLVM version 7.0.0 (clang-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.

c++ 11不是默认值。使用clang++,在c++ 11中编译下列代码是必要的:

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

在coderrunner 2中,您需要通过包含上述内容来修改属于c++的脚本。进入coderrunner> Preferences,然后在语言选项中选择c++,然后点击"Edit Script":

coderrunner - Preferences

您将在coderrunner中看到'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给我指出了正确的方向。

我可以确认问题是您的编译器不使用c++ 11模式。当使用Clang 3.4.1 编译代码时,没有-std=c++11 ,我得到与您完全相同的4个错误,但是这个命令行:

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

只给出这个警告:

警告:未使用的变量'z' [- unused-variable]
复z = 1;

coderrunner -> Preferences…-> Languages -> c++ -> Compile Flags:添加这个化c++ 11

最新更新