Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
like image 884
rcapac Avatar asked Dec 07 '25 02:12

rcapac


1 Answers

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.

like image 195
rcapac Avatar answered Dec 09 '25 21:12

rcapac