我试图让自己进入C ++并购买了Bjarne Stroustrup撰写的“Programming - Principles and Practice Using C ++”一书。
当我尝试编译以下源代码时:
#include "std_lib_facilities.h"
int main(){
cout<<"Hello, World!\n";
keep_window_open();
return 0;
}
我遇到编译错误:
In file included from /Users/hypertrooper/Documents/Programming - Principles and Practice Using C++/hello_world.cpp:1:
std_lib_facilities.h:71:20: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using size_type = typename std::vector<T>::size_type;
^
std_lib_facilities.h:102:20: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using size_type = std::string::size_type;
^
std_lib_facilities.h:107:8: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
if (i<0||size()<=i) throw Range_error(i);
~^~
std_lib_facilities.h:113:8: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
if (i<0||size()<=i) throw Range_error(i);
~^~
std_lib_facilities.h:213:107: error: expected '(' for function-style cast or type construction
inline int randint(int min, int max) { static default_random_engine ran; return uniform_int_distribution<>{min, max}(ran); }
~~~~~~~~~~~~~~~~~~~~~~~~~~^
std_lib_facilities.h:222:20: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using Value_type = typename C::value_type;
^
std_lib_facilities.h:225:18: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using Iterator = typename C::iterator;
^
6 warnings and 1 error generated.
我知道我的编译器没有使用C ++ 11功能,但我不知道如何更新编译器。我应该知道我正在使用MacOSX(10.10 Yosemite)并尝试使用xCode和textmate进行编译。我甚至尝试过这个教程(https://wiki.helsinki.fi/display/HUGG/Installing+the+GNU+compilers+on+Mac+OS+X),但它没有帮助。 (至少当我尝试使用文本配合进行编译时)
我希望你能帮到我。 :(
答案 0 :(得分:2)
如果您使用的是Mac或Linux,编译器通常是g ++或clang;要访问C ++ 11,只需在调用编译器时指定-std=c++11
作为选项(假设您有最新版本)。
答案 1 :(得分:0)
从构建设置中,将 C ++语言方言调整为C ++ 11 ...
还要确保使用LLVM C ++库,因为它具有完整的C ++ 11支持。
答案 2 :(得分:0)
您需要做的是打开项目设置 - &gt;构建设置并将C ++语言方言设置为C ++ 11,将C ++标准库设置为libc ++(支持C ++ 11的LLVM C ++标准库)。