C++

'std::unique_ptr' Is Not a Member of 'std' Fix

std::unique_ptr is a class template declared in the header , and the headers around pointer code do not declare it — not , not , and not . GCC helps here: its note names directly. The fix is to add the include the note points at.

Written November 27, 2026 · Tested with GCC 13.3.0 · 10 min read

By the end of this guide
  • The error: the include is missing
  • The same error through neighboring headers: <vector>, <string>, and <utility> are not enough
  • The fix
  • Creating with std::makeunique, and moving ownership

Written November 27, 2026

Tested with GCC 13.3.0

What this guide is

std::unique_ptr is a class template defined in the header <memory>. It owns and manages another object through a pointer and disposes of that object when the unique_ptr goes out of scope or is otherwise destroyed, and ownership can be transferred to another unique_ptr by moving, never by copying. The same header also declares std::shared_ptr, std::weak_ptr, and, since C++14, the std::make_unique function template.

Because the class template lives in one specific header, 'unique_ptr' is not a member of 'std' has one common cause:

  • The #include <memory> line is missing. For this error GCC does name the header: its note says 'std::unique_ptr' is defined in header '<memory>'; did you forget to '#include <memory>'?, and following that note is the whole fix.

The headers that sit near pointer code — <iostream>, <vector>, <string> — do not rescue the program. Each one is tested below, and each fails the same way.

The error: the include is missing

This program creates a std::unique_ptr to an integer but includes only <iostream>:

C++
#include <iostream>
int main() {
    std::unique_ptr<int> p(new int(42));
    std::cout << *p << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 noinclude.cpp -o noinclude_prog
noinclude.cpp: In function 'int main()':
noinclude.cpp:3:10: error: 'unique_ptr' is not a member of 'std'
    3 |     std::unique_ptr<int> p(new int(42));
      |          ^~~~~~~~~~
noinclude.cpp:2:1: note: 'std::unique_ptr' is defined in header '<memory>'; did you forget to '#include <memory>'?
    1 | #include <iostream>
  +++ |+#include <memory>
    2 | int main() {
noinclude.cpp:3:21: error: expected primary-expression before 'int'
    3 |     std::unique_ptr<int> p(new int(42));
      |                     ^~~
noinclude.cpp:4:19: error: 'p' was not declared in this scope
    4 |     std::cout << *p << "\n";
      |                   ^

The two errors after the note are knock-on effects of the first one: because the declaration of std::unique_ptr was never seen, the compiler cannot parse the declaration of p, and the later use of p has nothing to refer to. The default compilation mode prints the identical error, so this shape is not a language-mode problem. Fix the first error — the missing include — and the other two disappear with it.

The same error through neighboring headers: <vector>, <string>, and <utility> are not enough

Pointer code almost always already includes a container or string header, and those includes feel close enough. They are not. This program adds <string> and <vector> to the program above, and it fails the same way:

C++
#include <iostream>
#include <string>
#include <vector>
int main() {
    std::unique_ptr<int> p(new int(42));
    std::cout << *p << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 stringonly.cpp -o stringonly_prog
stringonly.cpp: In function 'int main()':
stringonly.cpp:5:10: error: 'unique_ptr' is not a member of 'std'
    5 |     std::unique_ptr<int> p(new int(42));
      |          ^~~~~~~~~~
stringonly.cpp:4:1: note: 'std::unique_ptr' is defined in header '<memory>'; did you forget to '#include <memory>'?
    3 | #include <vector>
  +++ |+#include <memory>
    4 | int main() {
stringonly.cpp:5:21: error: expected primary-expression before 'int'
    5 |     std::unique_ptr<int> p(new int(42));
      |                     ^~~
stringonly.cpp:6:19: error: 'p' was not declared in this scope
    6 |     std::cout << *p << "\n";
      |                   ^

The same result holds for the other neighboring headers tested, each with the same error and the same note naming <memory>:

  • With only <vector> and <iostream> included, the program fails with 'unique_ptr' is not a member of 'std'.
  • With only <utility> and <iostream> included, the program fails with 'unique_ptr' is not a member of 'std'.
  • With only <algorithm> and <iostream> included, the program fails with 'unique_ptr' is not a member of 'std'.
  • With only <functional> and <iostream> included, the program fails with 'unique_ptr' is not a member of 'std'.

No neighboring header rescues this program on GCC 13.3.0 — there is no transitive-include exception here. No standard header is required to declare std::unique_ptr on behalf of <memory>, and a program that compiled only because some other header happened to pull the declaration in as a side effect could break under a different library version. Include <memory> in every file that names std::unique_ptr, std::shared_ptr, std::weak_ptr, or std::make_unique.

The fix

Add the header:

C++
#include <iostream>
#include <memory>
int main() {
    std::unique_ptr<int> p(new int(42));
    std::cout << *p << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 good.cpp -o good_prog
$ ./good_prog
42

The same program also compiles and runs with no -std flag at all. The unique_ptr owns the integer created by new int(42), *p reads the owned value, and the integer is deleted automatically when p goes out of scope at the end of main — there is no delete to write.

Creating with std::make_unique, and moving ownership

std::make_unique is a function template, also defined in <memory>, that constructs an object of the given type and wraps it in a std::unique_ptr. It is the usual way to create a unique_ptr in new code, because the allocation never appears as a bare new expression:

C++
#include <iostream>
#include <memory>
int main() {
    auto p = std::make_unique<int>(7);
    std::cout << *p << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 make_unique.cpp -o make_unique_prog
$ ./make_unique_prog
7

A unique_ptr cannot be copied, but it can be moved: the move transfers ownership to the destination pointer and leaves the source empty. This program moves ownership from p to q and prints both the state of the source and the value now owned by the destination:

C++
#include <iostream>
#include <memory>
int main() {
    auto p = std::make_unique<int>(10);
    auto q = std::move(p);
    std::cout << (p ? "p owns" : "p is empty") << "\n";
    std::cout << *q << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 move.cpp -o move_prog
$ ./move_prog
p is empty
10

After the move, dereferencing p would be a defect — it owns nothing — while q owns the integer and deletes it at scope exit. std::move itself is declared in <utility>, but in this program <memory> is the header that declares the pointer type being moved.

Destruction at scope exit, and the array form

The owned object is destroyed when the unique_ptr goes out of scope, which is visible when the owned type has a destructor that prints. This program creates a unique_ptr to a small struct inside a block, and the destructor runs as the block ends, before the final line prints:

C++
#include <iostream>
#include <memory>
struct Item {
    int value;
    ~Item() { std::cout << "destroyed " << value << "\n"; }
};
int main() {
    {
        auto p = std::make_unique<Item>();
        p->value = 5;
        std::cout << p->value << "\n";
    }
    std::cout << "done\n";
    return 0;
}
Terminal
$ g++ -std=c++17 struct.cpp -o struct_prog
$ ./struct_prog
5
destroyed 5
done

std::unique_ptr also has an array form, created here with the array overload of std::make_unique, whose elements are reached with operator[] and deleted together when the pointer goes out of scope:

C++
#include <iostream>
#include <memory>
int main() {
    auto a = std::make_unique<int[]>(3);
    a[0] = 1;
    a[1] = 2;
    a[2] = 3;
    for (int i = 0; i < 3; ++i) {
        if (i) std::cout << " ";
        std::cout << a[i];
    }
    std::cout << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 array.cpp -o array_prog
$ ./array_prog
1 2 3

Both programs use the same header as the integer case: one include, <memory>, declares the class template, the factory function, and the array specialization.

The neighboring errors: std::shared_ptr, std::weak_ptr, and std::make_unique

The same missing include is worded for whichever <memory> name the program uses. With std::shared_ptr — the shared-ownership smart pointer, also defined in <memory> — the error names that class instead, with the same note:

C++
#include <iostream>
int main() {
    std::shared_ptr<int> p(new int(42));
    std::cout << *p << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 shared_noinclude.cpp -o shared_noinclude_prog
shared_noinclude.cpp: In function 'int main()':
shared_noinclude.cpp:3:10: error: 'shared_ptr' is not a member of 'std'
    3 |     std::shared_ptr<int> p(new int(42));
      |          ^~~~~~~~~~
shared_noinclude.cpp:2:1: note: 'std::shared_ptr' is defined in header '<memory>'; did you forget to '#include <memory>'?
    1 | #include <iostream>
  +++ |+#include <memory>
    2 | int main() {
shared_noinclude.cpp:3:21: error: expected primary-expression before 'int'
    3 |     std::shared_ptr<int> p(new int(42));
      |                     ^~~
shared_noinclude.cpp:4:19: error: 'p' was not declared in this scope
    4 |     std::cout << *p << "\n";
      |                   ^

Calling std::make_unique without the include fails the same way, naming the function template:

C++
#include <iostream>
int main() {
    auto p = std::make_unique<int>(7);
    std::cout << *p << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 make_unique_noinclude.cpp -o make_unique_noinclude_prog
make_unique_noinclude.cpp: In function 'int main()':
make_unique_noinclude.cpp:3:19: error: 'make_unique' is not a member of 'std'
    3 |     auto p = std::make_unique<int>(7);
      |                   ^~~~~~~~~~~
make_unique_noinclude.cpp:2:1: note: 'std::make_unique' is defined in header '<memory>'; did you forget to '#include <memory>'?
    1 | #include <iostream>
  +++ |+#include <memory>
    2 | int main() {
make_unique_noinclude.cpp:3:31: error: expected primary-expression before 'int'
    3 |     auto p = std::make_unique<int>(7);
      |                               ^~~

std::weak_ptr — the non-owning observer of a std::shared_ptr-managed object, also defined in <memory> — fails identically, with 'weak_ptr' is not a member of 'std' and the same note naming <memory>. All three programs compile and run once <memory> is added: the shared_ptr program prints 42, the make_unique program prints 7, and a default-constructed std::weak_ptr<int> reports w.expired() as 1, because it observes no object.

One language-mode exception applies to std::make_unique only: in C++11 mode, with <memory> included, the call still fails with 'make_unique' is not a member of 'std', but the note is different — 'std::make_unique' is only available from C++14 onwards. That wording is not a missing include; it is the language mode, and the fix is to compile as C++14 or later.

A different wording: unique_ptr without the std::

Calling the class without its namespace qualifier is a different defect, and GCC words it differently:

C++
#include <iostream>
#include <memory>
int main() {
    unique_ptr<int> p(new int(42));
    std::cout << *p << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 unqualified.cpp -o unqualified_prog
unqualified.cpp: In function 'int main()':
unqualified.cpp:4:5: error: 'unique_ptr' was not declared in this scope; did you mean 'std::unique_ptr'?
    4 |     unique_ptr<int> p(new int(42));
      |     ^~~~~~~~~~
      |     std::unique_ptr
In file included from /usr/include/c++/13/memory:78,
                 from unqualified.cpp:2:
/usr/include/c++/13/bits/unique_ptr.h:277:11: note: 'std::unique_ptr' declared here
  277 |     class unique_ptr
      |           ^~~~~~~~~~
unqualified.cpp:4:16: error: expected primary-expression before 'int'
    4 |     unique_ptr<int> p(new int(42));
      |                ^~~
unqualified.cpp:5:19: error: 'p' was not declared in this scope
    5 |     std::cout << *p << "\n";
      |                   ^

'unique_ptr' was not declared in this scope is the 'has not been declared' family for this topic: an unqualified name that lookup cannot find at all. This program already includes <memory>, so the include is not the problem — the declaration it adds is std::unique_ptr, inside namespace std, and the use must be qualified as std::unique_ptr, or the file must bring the name in deliberately. If your error says is not a member of 'std', the name was qualified and the include is the whole problem; if it says was not declared in this scope, check the qualifier first.

The exact rule

  • std::unique_ptr, std::shared_ptr, std::weak_ptr, and std::make_unique are declared by #include <memory>, and nowhere else by rule. <iostream>, <vector>, <string>, <utility>, <algorithm>, and <functional> do not declare std::unique_ptr.
  • 'unique_ptr' is not a member of 'std' means the <memory> include is missing, and GCC's note names the header directly: 'std::unique_ptr' is defined in header '<memory>'; did you forget to '#include <memory>'? Follow the note.
  • The expected primary-expression before 'int' and 'p' was not declared in this scope errors that follow are knock-on effects of the missing declaration, not separate defects.
  • 'unique_ptr' was not declared in this scope for an unqualified unique_ptr(...) use is a different defect: qualify the name as std::unique_ptr — the include alone does not bring the name out of namespace std.
  • 'make_unique' is not a member of 'std' with the note 'std::make_unique' is only available from C++14 onwards is a language-mode problem, not an include problem: compile as C++14 or later.

The fixes, ranked

  • First, check the include list of the file that names std::unique_ptr. If <memory> is not there, add it — that is the fix in the common case, and it is the fix GCC's own note names.
  • Second, do not chase the follow-on errors. Fix the include and recompile; the parse error and the undeclared-p error are consequences, not causes.
  • Third, if the error says was not declared in this scope instead, fix the name's qualifier to std::unique_ptr as well.
  • Fourth, if the failing name is std::make_unique and <memory> is already included, check the language mode: std::make_unique needs C++14 or later.

Which fix for which cause

  • #include <memory> missing, std::unique_ptr used with <iostream> present: the message is 'unique_ptr' is not a member of 'std' — add #include <memory>.
  • Only <vector>, <string>, <utility>, <algorithm>, or <functional> included: same message, same note — add #include <memory>.
  • std::shared_ptr used without <memory>: the message is 'shared_ptr' is not a member of 'std' — add #include <memory>.
  • std::weak_ptr used without <memory>: the message is 'weak_ptr' is not a member of 'std' — add #include <memory>.
  • std::make_unique called without <memory>: the message is 'make_unique' is not a member of 'std' — add #include <memory>.
  • std::make_unique called in C++11 mode with <memory> included: the message is 'make_unique' is not a member of 'std' with the note 'std::make_unique' is only available from C++14 onwards — compile as C++14 or later.
  • unique_ptr(...) used without the std:: qualifier: the message is 'unique_ptr' was not declared in this scope — qualify the name as std::unique_ptr and include <memory>.

Checklist

  • Check the include list of the file that names the smart pointer, not just the project. The include must be in that file, or in a header that file includes directly by design.
  • Read the note under the error. For this error GCC names the header — 'std::unique_ptr' is defined in header '<memory>' — so take the fix from the note instead of guessing.
  • Do not rely on a neighboring header pulling std::unique_ptr in. None of <iostream>, <vector>, <string>, <utility>, <algorithm>, or <functional> does on this toolchain, and no header is required to.
  • If the program also moves a unique_ptr with std::move, that name comes from <utility> — one header per name the file uses, even when a program happens to compile without it.
  • After the fix, confirm the program runs, not just that it compiles: the fixed programs here print 42, 7, p is empty followed by 10, 5 followed by destroyed 5 and done, and 1 2 3.
  • If shared ownership is genuinely needed, switch to std::shared_ptr — same header, same fix, different ownership rule. If one owner is enough, std::unique_ptr is the cheaper default.

Sources

  • cppreference, std::unique_ptr — for std::unique_ptr being defined in <memory>, for disposing of the owned object when the unique_ptr is destroyed, and for ownership being transferable by moving but not by copying.
  • cppreference, Standard library header <memory> — for <memory> declaring the smart pointer class templates and the make_unique factory functions.
  • cppreference, std::make_unique — for std::make_unique being defined in <memory> since C++14, constructing an object of the given type, and returning a std::unique_ptr that owns it, including the array overload.
  • cppreference, std::shared_ptr — for std::shared_ptr being defined in <memory> and retaining shared ownership of an object through a pointer.
  • cppreference, std::weak_ptr — for std::weak_ptr being defined in <memory>, observing a std::shared_ptr-managed object without owning it, and expired() reporting whether the observed object has been deleted.

Keep Exploring