How can I compile C++11 code with Orwell Dev-C++?

时光毁灭记忆、已成空白 提交于 2019-11-26 17:19:08

问题


Trying to compile the following code:

#include <iostream>
#include <memory>

struct Foo {
    Foo() { std::cout << "Foo::Foo\n"; }
    ~Foo() { std::cout << "Foo::~Foo\n"; }
    void bar() { std::cout << "Foo::bar\n"; }
};

void f(const Foo &foo)
{
    std::cout << "f(const Foo&)\n";
}

int main()
{
    std::unique_ptr<Foo> p1(new Foo);  // p1 owns Foo
    if (p1) p1->bar();

    {
        std::unique_ptr<Foo> p2(std::move(p1));  // now p2 owns Foo
        f(*p2);

        p1 = std::move(p2);  // ownership returns to p1
        std::cout << "destroying p2...\n";
    }

    if (p1) p1->bar();

    // Foo instance is destroyed when p1 goes out of scope
}

with Orwell Dev-c++ 5.3.0.3 yields the following error:

'unique_ptr' is not a member of 'std'.

How can I handle this?


回答1:


Please make sure you supply the correct -std flag when compiling. The default setting that Orwell Dev-C++ uses (don't pass any -std option), will not enable some shiny new C++11 functions, like unique_ptr. The fix is quite simple:

  • For non-project compilations, go to: Tools >> Compiler Options >> (select your compiler) >> Settings >> Code Generation >> (set 'Language standard' to a C++11 option)
  • For project compilations, go to: Project >> Compiler >> Code Generation >> (set 'Language standard' to a C++11 option)

Here's a bit more information about the -std flag: http://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html#C-Dialect-Options

As you can see, GCC uses a GNU dialect of C++03 by default (which doesn't seem to support unique_ptr).



来源:https://stackoverflow.com/questions/13613295/how-can-i-compile-c11-code-with-orwell-dev-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!