如何更新我的编译器以使用 C++11 功能?

2 c++ xcode textmate c++11

我试图让自己进入 C++ 并购买了 Bjarne Stroustrup 所著的“编程 - 使用 C++ 的原则和实践”一书。

当我尝试编译以下源代码时:

#include "std_lib_facilities.h"
int main(){
    cout<<"Hello, World!\n";
    keep_window_open();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我收到以下编译错误:

    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.
Run Code Online (Sandbox Code Playgroud)

我知道我的编译器没有使用 C++11 功能,但我不知道如何更新编译器。我应该知道我正在使用 MacOSX(10.10 Yosemite)并尝试使用 xCode 和 textmate 进行编译。我什至尝试遵循本教程(https://wiki.helsinki.fi/display/HUGG/Installing+the+GNU+compilers+on+Mac+OS+X),但没有帮助。(至少当我尝试用文本伙伴编译时)

我希望你能帮助我。:(

Jam*_*nze 6

如果你在 Mac 或 Linux 上,编译器通常是 g++ 或 clang;要访问 C++11,只需-std=c++11在调用编译器时指定为选项(假设您有最新版本)。