unique_ptr compile error
28,955
Solution 1
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
.
Solution 2
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)
Author by
rahman
Updated on July 09, 2022Comments
-
rahman almost 2 years
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
-
Montreal almost 9 yearsI am facing the same problem, but
-std=c++11
flag is enabled and all required headers are included. Can't get what's wrong: gist.github.com/canadien91/2ba3f9576823159c2d52 -
Stuck about 7 yearsfor future reference: @Montreal #include <memory>
-
Montreal about 7 years@Stuck, hmm, in my gist <memory> is included. But I don't remember now when I have added this.
-
Stuck about 7 yearsactually the gist link for me is a 404 error, so I only had a guess.. because I experienced the same problem. For me it would have been helpful to get that hint here beneath your comment.
-
Daniel Stevens over 6 yearsWorth noting, the std::make_unique function requires c++14