问题
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