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

3
我是一名新手程序员,正在自学C++,使用的是Bjarne的书籍《C++11版本》。我在OS X El Cap上安装了Xcode命令行工具,并使用Coderunner 2进行编程。当使用初始化列表创建变量时,我的代码出现了错误。我认为这是因为Coderunner没有运行C++11。作为一个完全的新手,我不知道该怎么办。希望得到有用的建议。谢谢。
clang版本:Apple LLVM版本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;
    }

I get the following error:

    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.
3个回答

5

C++11不是默认的选项。使用clang++编译器,需要以下命令来编译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指引我正确的方向。

1
我可以确认问题是你的编译器没有使用C++11模式。当我使用Clang 3.4.1编译你的代码没有加上-std=c++11时,我得到了和你一模一样的四个错误,但是加上这条命令行:
 c++ -stc=c++11 -c -Wall -pedantic foo.cpp

仅提示以下警告:

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


1

CodeRunner -> 首选项... -> 语言选项卡 -> C++ -> 编译标志: 添加以下内容 -std=c++11 这对我有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接