我是Bjarne的书C ++ 11版本的新编程和自学C ++的新手。我正在使用Coderunner 2和OS X El Cap上安装的Xcode命令行工具。使用初始化列表创建变量时,我会得到以下代码的错误。我的信念是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;
}
我收到以下错误:
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++
在Coderunner 2中,您需要通过包含上述内容来修改与c ++相关的脚本。转到Coderunner> Preferences,然后选择C ++并单击'Edit Script':
您将在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指出我正确的方向。
我可以确认问题是你的编译器没有使用C ++ 11模式。使用没有-std=c++11
的Clang 3.4.1编译代码时,我得到与您完全相同的4个错误,但是这个命令行:
c++ -stc=c++11 -c -Wall -pedantic foo.cpp
只发出此警告:
警告:未使用的变量'z'[-Wunused-variable] 复数z = 1;
CodeRunner - > Preferences ... - > Languages Tab - > C ++ - > Compile Flags:add this -std = c ++ 11这对我有用。