rahman
rahman

Reputation: 4948

unique_ptr compile error

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

#include<memory>
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

Upvotes: 20

Views: 30979

Answers (2)

BЈовић
BЈовић

Reputation: 64203

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 newer version of c++ :

g++ -std=c++11

or

g++ -std=c++14

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

Upvotes: 32

Sevener
Sevener

Reputation: 41

If you are using Code::Blocks, go to Settings > Compiler > Global compiler settings > Compiler settings and look for the Have g++ follow the C++11 ISO C++ language standard [-std=c++11] and check it!

(Code::Blocks will add the -std=c++11 for you when compiling)

Upvotes: 4

Related Questions