rahman rahman - 1 year ago 54
Linux Question

unique_ptr compile error

I guess this is embarrassing if I told you I cant get this to compile. would you please help me:

using namespace std;

int main()
std::unique_ptr<int> p1(new int(5));
return 0;

$ gcc main.cpp
main.cpp: In function ‘int main()’:
main.cpp:6:2: error: ‘unique_ptr’ was not declared in this scope
main.cpp:6:13: error: expected primary-expression before ‘int’
main.cpp:6:13: error: expected ‘;’ before ‘int’

$ gcc --version
gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1

Answer Source

This is just a guess.

Most likely you compiled your program like this (or similarly) :

g++ main.cpp

If you did, then the problem is that g++ uses c++03 as default. To use c++11 features (and std::unique_ptr), you need to use c++11:

g++ -std=c++0x

and I would recommend to use also -Wall -Wextra -pedantic.