简体   繁体   中英

Coderunner 2 - Initializing List Errors - C++11

I'm new to programming and teaching myself C++ with Bjarne's book, C++11 version. I'm using Coderunner 2 with Xcode command-line tools installed on OS X El Cap. I get errors for the following code when creating variables using initializer lists. My belief is Coderunner isn't running c++11. I'm a complete novice and I don't know what to do for the life of me. Helpful advice is appreciated. Thank you in advance.

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;
    }

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.

C++11 is not default. Using clang++, the following is necessary to compile in C++11:

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

In Coderunner 2, you need to modify the script that pertains to c++ by including the above. Go to Coderunner > Preferences, then for Language choose C++ and click on 'Edit Script':

Coderunner - Preferences

You will see the 'compile.sh' file in Coderunner. Modify line 78:

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

Modify line 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}"

Hope that helps! Thank you Serge Ballesta for pointing me in the right direction.

I can confirm that the problem is your compiler not using the C++11 mode. When compiling your code with Clang 3.4.1 without -std=c++11 I get the exact same 4 errors as you, but this command line:

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

only gives this warning:

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

CodeRunner - > Preferences ... - > Languages Tab - > C ++ - > Compile Flags:add this -std = c ++ 11这对我有用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM