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

Written November 28, 2026 · Tested with GCC 13.3.0 · 11 min read

By the end of this guide
  • The error: the include is missing
  • Neighboring headers: six fail, none rescues
  • The fix
  • Sharing ownership, counting owners, and watching weakly

Written November 28, 2026

Tested with GCC 13.3.0

What this guide is

std::shared_ptr is a class template, since C++11, defined in the header <memory>. It is a smart pointer that retains shared ownership of an object: several shared pointers may own the same object, and the object is destroyed when the last remaining owner is destroyed or is assigned another pointer. The factory function std::make_shared, also since C++11 and in the same header, allocates an object and returns a shared pointer managing it, with a use count of 1.

Because both names live in that one header, 'shared_ptr' is not a member of 'std' in a program that never included it has one common cause:

  • The #include <memory> line is missing. For this error GCC does print a note naming the header, and even shows the include line to add. The fix is that include.

No neighboring header pulls this name in, not even by accident on this library, and one section below shows the sweep that settles that.

The error: the include is missing

This program creates a shared pointer to an integer, prints the value, and prints how many owners it has, but includes only <iostream>:

C++
#include <iostream>
int main() {
    std::shared_ptr<int> value = std::make_shared<int>(42);
    std::cout << *value << "\n";
    std::cout << value.use_count() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 a_noinclude.cpp -o a_prog
a_noinclude.cpp: In function 'int main()':
a_noinclude.cpp:3:10: error: 'shared_ptr' is not a member of 'std'
    3 |     std::shared_ptr<int> value = std::make_shared<int>(42);
      |          ^~~~~~~~~~
a_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() {
a_noinclude.cpp:3:21: error: expected primary-expression before 'int'
    3 |     std::shared_ptr<int> value = std::make_shared<int>(42);
      |                     ^~~
a_noinclude.cpp:4:19: error: 'value' was not declared in this scope
    4 |     std::cout << *value << "\n";
      |                   ^~~~~

Read it in order. The first error is the missing name itself. The note under it names <memory> as the header that defines the name, and the snippet under the note shows the include line to add, marked with +++. The two errors after that are fallout, not separate defects: with the type unknown, the <int> after it has nothing to attach to, and the variable the line was meant to declare was never declared, so its first use fails too. Fix the include and all three errors go together.

Neighboring headers: six fail, none rescues

Swapping in another nearby header changes nothing here. Compiled one at a time as the only extra header besides <iostream>, each of these fails with the same first error, 'shared_ptr' is not a member of 'std', and the same note naming <memory>:

  • <vector>
  • <string>
  • <algorithm>
  • <utility>
  • <functional>
  • <thread>

That list includes a broad utility header, <functional>, and <thread>, a header whose own machinery deals in shared state. None of them declares the name for the program on this libstdc++, not even transitively. There is no accidental rescue to stumble into here: a program that uses this pointer includes <memory>.

The fix

Add the header:

C++
#include <iostream>
#include <memory>
int main() {
    std::shared_ptr<int> value = std::make_shared<int>(42);
    std::cout << *value << "\n";
    std::cout << value.use_count() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 a_fixed.cpp -o a_fixed_prog
$ ./a_fixed_prog
42
1

One include, <memory>, declares both names the program uses. The factory call builds the integer 42 and returns the pointer that manages it, dereferencing the pointer prints the value, and the use count is 1 because the returned pointer is, at that point, the only owner.

Sharing ownership, counting owners, and watching weakly

Copying a shared pointer shares ownership instead of copying the object, and the count follows the copies. A std::weak_ptr watches the object without owning it: it must be converted back to a shared pointer, through its lock member, in order to reach the object, and its expired member reports whether the object has already been deleted:

C++
#include <iostream>
#include <memory>
int main() {
    std::shared_ptr<int> first = std::make_shared<int>(42);
    std::cout << first.use_count() << " " << *first << "\n";
    std::shared_ptr<int> second = first;
    std::cout << first.use_count() << " " << second.use_count() << "\n";
    std::weak_ptr<int> watcher = first;
    std::cout << watcher.use_count() << " " << watcher.expired() << "\n";
    second.reset();
    std::cout << first.use_count() << "\n";
    if (auto locked = watcher.lock()) {
        std::cout << "locked " << *locked << "\n";
    }
    first.reset();
    std::cout << watcher.expired() << "\n";
    if (auto locked = watcher.lock()) {
        std::cout << "locked " << *locked << "\n";
    } else {
        std::cout << "lock empty\n";
    }
    return 0;
}
Terminal
$ g++ -std=c++20 b_rich.cpp -o b_prog
$ ./b_prog
1 42
2 2
2 0
1
locked 42
1
lock empty

Read line by line. The count starts at 1. The copy raises it to 2, and both owners report the same count, because there is one object and one count for it. The weak pointer reports that same count, 2, and expired prints 0 — the object is alive, and the weak pointer itself added no owner. Resetting the copy drops the count back to 1. While an owner remains, lock returns a pointer that reaches the value, 42. After the last owner resets, expired prints 1, and lock returns an empty pointer, so the program takes the other branch and prints lock empty instead of dereferencing anything.

A custom deleter, and what reset does to it

A shared pointer can be given a deleter of the program's own at construction, and that deleter is what destroys the object when the last owner lets go. This program supplies a deleter that announces itself, then replaces the managed object with reset:

C++
#include <iostream>
#include <memory>
int main() {
    std::shared_ptr<int> value(new int(7), [](int* p) {
        std::cout << "deleter " << *p << "\n";
        delete p;
    });
    std::cout << *value << " " << value.use_count() << "\n";
    value.reset(new int(9));
    std::cout << *value << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 c_deleter.cpp -o c_prog
$ ./c_prog
7 1
deleter 7
9

The output holds a quiet lesson. Replacing the object through reset runs the supplied deleter on the old object, so deleter 7 prints between the two values. But no deleter 9 line ever prints: the replacement pointer given to reset is a fresh ownership, deleted the ordinary way at the end of the program, not by the deleter that was supplied for the first object. A deleter belongs to the ownership it was supplied with, not to the pointer variable that happens to hold it later.

The same failure on the factory function alone

A program does not have to name the type to hit this header. This one names only the factory function, letting auto take the type, and includes only <iostream>:

C++
#include <iostream>
int main() {
    auto value = std::make_shared<int>(42);
    std::cout << *value << "\n";
    std::cout << value.use_count() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 e_makeonly.cpp -o e_prog
e_makeonly.cpp: In function 'int main()':
e_makeonly.cpp:3:23: error: 'make_shared' is not a member of 'std'
    3 |     auto value = std::make_shared<int>(42);
      |                       ^~~~~~~~~~~
e_makeonly.cpp:2:1: note: 'std::make_shared' is defined in header '<memory>'; did you forget to '#include <memory>'?
    1 | #include <iostream>
  +++ |+#include <memory>
    2 | int main() {
e_makeonly.cpp:3:35: error: expected primary-expression before 'int'
    3 |     auto value = std::make_shared<int>(42);
      |                                   ^~~

The shape is the same, one name over: 'make_shared' is not a member of 'std', the same note naming <memory>, and the same follow-on error for the orphaned <int>. Adding the include fixes this program too — the factory and the type are declared by the same header, so a file that uses either one includes it.

Same first error, different note: the build is in C++98 mode

The name exists only from C++11. Take the fixed program, with <memory> included, and compile it in C++98 mode instead:

C++
#include <iostream>
#include <memory>
int main() {
    std::shared_ptr<int> value = std::make_shared<int>(42);
    std::cout << *value << "\n";
    std::cout << value.use_count() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++98 a_fixed.cpp -o x98_prog
a_fixed.cpp: In function 'int main()':
a_fixed.cpp:4:10: error: 'shared_ptr' is not a member of 'std'
    4 |     std::shared_ptr<int> value = std::make_shared<int>(42);
      |          ^~~~~~~~~~
a_fixed.cpp:4:10: note: 'std::shared_ptr' is only available from C++11 onwards
a_fixed.cpp:4:21: error: expected primary-expression before 'int'
    4 |     std::shared_ptr<int> value = std::make_shared<int>(42);
      |                     ^~~
a_fixed.cpp:5:19: error: 'value' was not declared in this scope
    5 |     std::cout << *value << "\n";
      |                   ^~~~~

The first error line is word-for-word the same, but the note under it is a different one: not a header note, an availability note, saying the name exists only from C++11 onwards. The include is present in this build and cannot help, because the language mode predates the name. So when the note names a header, add the header; when the note names a language version, raise the mode to C++11 or later — and when both could be wrong, the note is what tells them apart here.

A different wording: the name without its qualifier

Including <memory> is not the only half of the name. This program includes the header but drops the std:: qualifier from the type:

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

That wording — was not declared in this scope — is a different defect with a different fix: the header is present, the qualifier is not, and the compiler suggests the qualified name and points at its declaration. Qualify the name; adding includes does not bring the name out of namespace std.

The exact rule

  • The shared pointer, the weak pointer, and the factory function are declared by #include <memory>, and that is the only header required to declare them. <vector>, <string>, <algorithm>, <utility>, <functional>, and <thread> do not declare the shared pointer name, on this library not even transitively.
  • 'shared_ptr' is not a member of 'std' in a program that includes only <iostream> beside the use means the <memory> include is missing, and GCC's note names that header and shows the line to add. The follow-on errors for the template argument and the undeclared variable are fallout from the same missing name.
  • 'make_shared' is not a member of 'std' is the same defect seen from the factory function alone, with the same header note and the same fix.
  • Copying a shared pointer shares ownership of one object and raises its use count; reset on one owner lowers the count, and the object is destroyed when the last owner is destroyed or reset.
  • A weak pointer adds no owner. Its expired member reports whether the managed object has already been deleted, and lock returns a shared pointer to the object while it lives and an empty pointer after it is gone.
  • A custom deleter supplied at construction destroys the object that ownership was created with. A replacement object handed to reset is a fresh ownership and is not destroyed by the earlier deleter.
  • In a C++98 build, the same first error occurs with <memory> included, with an availability note instead of a header note. The mode has to be C++11 or later, and the note is what separates the two causes.
  • 'shared_ptr' was not declared in this scope for an unqualified use is a different defect: qualify the name — the include alone does not bring the name out of namespace std.
  • Ownership of an object is shared only by copying or assigning an existing shared pointer. Building a second, separate shared pointer from the same raw pointer does not share ownership — the two owners each believe the object is theirs to delete.

The fixes, ranked

  • First, read the note under the first error. If it names <memory>, add #include <memory> to the file that names the pointer — that is the common case, and it clears the follow-on errors with it.
  • Second, if the note instead says the name is only available from C++11 onwards, leave the includes alone and raise the build mode to C++11 or later.
  • Third, do not substitute a neighboring header. <vector>, <string>, <algorithm>, <utility>, <functional>, and <thread> were each tried here and each fails; none of them pulls the name in by accident on this library.
  • Fourth, if the error says was not declared in this scope instead, fix the name's qualifier as well.
  • Fifth, share ownership by copying an existing shared pointer, never by constructing a second one from the same raw pointer — and where a deleter must run for a replacement object too, supply it with that replacement's own construction or reset call, not the first one's.

Which fix for which cause

  • #include <memory> missing, the pointer used in a program with <iostream> present: the message is 'shared_ptr' is not a member of 'std' with a note naming <memory> — add #include <memory>.
  • Only <vector>, <string>, <algorithm>, <utility>, <functional>, or <thread> included: same first error, same note — add #include <memory>.
  • The factory function used alone, with auto, and no <memory>: the message is 'make_shared' is not a member of 'std' with the same note — add #include <memory>.
  • Build in C++98 mode, with or without #include <memory>: the same first error, with a note saying the name is only available from C++11 onwards — switch the build to C++11 mode or later, and keep the include.
  • The name used without its qualifier, <memory> included: the message is 'shared_ptr' was not declared in this scope — qualify the name.
  • A custom deleter does not run for an object installed by reset: the deleter belonged to the replaced ownership — supply the deleter with the ownership that should use it.

Checklist

  • Check the include list of the file that names the 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 before editing: a header note and an availability note sit under the same first error line and call for different fixes.
  • Do not chase the follow-on errors. An unknown template name produces an argument error and an undeclared-variable error behind it; all of them clear with the one fix.
  • Do not rely on a neighboring header pulling the name in. All six headers tried here fail, including <thread>.
  • Confirm the build mode is C++11 or later. The name does not exist in C++98 mode, with or without its header.
  • After the fix, confirm the program runs, not just that it compiles: the fixed program here prints 42 and a use count of 1.
  • Share ownership by copying, and watch objects weakly where a watcher must not keep the object alive: a weak pointer that reports expired is telling the truth, and locking it is the safe way back in.

Sources

  • cppreference, std::shared_ptr — for the shared pointer (since C++11) retaining shared ownership of an object, for the object being destroyed when the last remaining owner is destroyed or assigned another pointer through assignment or reset, for destruction by a delete expression or a custom deleter supplied during construction, for use_count returning the number of shared pointers referring to the same managed object, and for ownership being shared only by copying or assigning an existing shared pointer, with a new pointer built from another's raw pointer being undefined behavior.
  • cppreference, Standard library header <memory> — for <memory> as the header declaring the smart pointer facilities, including the shared and weak pointers and the factory functions.
  • cppreference, std::make_shared — for the factory function (since C++11) being defined in header <memory>, allocating and initializing an object and returning a shared pointer managing it, for the returned pointer's use count being 1, and for the factory not allowing a custom deleter, unlike the constructors.
  • cppreference, std::weak_ptr — for the weak pointer (since C++11) holding a non-owning reference to an object managed by a shared pointer, for conversion back through lock being required to access the object, for expired checking whether the referenced object was already deleted, for use_count returning the number of shared pointers that manage the object, and for weak pointers breaking reference cycles between shared pointers.

Keep Exploring