3

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?

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
Fihop
  • 3,127
  • 9
  • 42
  • 65
  • I don't have the mentioned IDE, but I suspect the code example could be trimmed down quite a bit, possibly to something like `#include int main () { std::unique_ptr p;}`. That would greatly reduce the unnecessary clutter in your post. – Baum mit Augen Jan 22 '16 at 03:17
  • Install a recent C++11 compatible compiler, e.g. [GCC](http://gcc.gnu.org/) – Basile Starynkevitch Feb 26 '16 at 17:31

2 Answers2

11

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).

Orwell
  • 1,468
  • 1
  • 13
  • 28
  • I have one more problem when selecting Language standard as GNU C++11, it gives me "unrecognized command line option "-std=c++0x" – Fihop Nov 29 '12 at 22:48
  • Try selecting the GNU C++ standard. Looks like I need to update the ISO C++11 flags to 'c++11' from 'c++0x'. – Orwell Nov 30 '12 at 17:09
  • 1
    Have you been able to get this working ? I installed the latest Dev-C++ 5.4.2 , but still get "unrecognized command line option "-std=c++0x" errors when I try to enable c++11. – Strahd_za May 29 '13 at 12:23
0

The latest version of Dev-C++ comes with TDM-GCC 9.2.0 which defaults to C++17.

FMXExpress
  • 1,246
  • 8
  • 10