C++

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

std::filesystem lives in one header, , and in one language mode, C++17 or later. Miss either half and the namespace is not there: the compiler reports the name as not declared, or as not naming a type, depending on where the failing line sits.

Written November 29, 2026 · Tested with GCC 13.3.0 · 8 min read

By the end of this guide
  • The rule underneath: one header, one language mode
  • The first failure: no <filesystem> include
  • The second failure: <filesystem> included, compiled as C++14
  • The header sweep: neighboring headers do not substitute

Written November 29, 2026

Tested with GCC 13.3.0

What this guide is

The error on a std::filesystem name means the compiler has no filesystem namespace inside std to look the name up in. That happens for exactly two reasons, and this guide reproduces both. The first is a missing include: nothing in the file included <filesystem>, the one header that declares the namespace, so every std::filesystem:: name in the file fails. The second is the language mode: std::filesystem is a C++17 library, so a file that includes <filesystem> and is compiled at -std=c++14 fails the same way - in that mode the header declares no std::filesystem names at all.

The wording changes with where the failing name sits, and both wordings appear below. Inside a function, GCC reports 'std::filesystem' has not been declared. At namespace scope, where the name is used as a type, it reports 'filesystem' in namespace 'std' does not name a type. They are one fault - the namespace is absent - read at two different positions in the file. The fix is the pair together: include <filesystem> and compile at C++17 or later.

The rule underneath: one header, one language mode

The Filesystem library page dates the whole library: the Filesystem library is since C++17, merged into ISO C++ as of C++17 from the technical specification ISO/IEC TS 18822:2015, and its feature-test macro __cpp_lib_filesystem has the value 201703L for C++17. There is no C++14 form of std::filesystem - the pre-standard version lived in a different namespace, std::experimental::filesystem, under a different header.

The header page shows what the header declares, all of it in namespace std::filesystem: the classes path, filesystem_error, directory_entry and directory_iterator, and the operations exists, file_size, current_path, create_directory and the rest, each marked (C++17). The std::filesystem::path page gives the central class the same date: class path since C++17, representing a path on a filesystem, with decomposition members such as filename() returning the filename component. So a std::filesystem name needs both conditions at once: the header included, so the declarations are in the file, and a language mode of C++17 or later, so the header declares them.

The first failure: no <filesystem> include

This program uses std::filesystem::path and std::filesystem::exists and includes only <iostream> and <string>:

C++
#include <iostream>
#include <string>

int main() {
    std::filesystem::path p{"data.txt"};
    bool e = std::filesystem::exists(p);
    std::cout << e << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 p1_fail_no_include.cpp -o p1_fail_no_include
p1_fail_no_include.cpp: In function 'int main()':
p1_fail_no_include.cpp:5:10: error: 'std::filesystem' has not been declared
    5 |     std::filesystem::path p{"data.txt"};
      |          ^~~~~~~~~~
p1_fail_no_include.cpp:6:19: error: 'std::filesystem' has not been declared
    6 |     bool e = std::filesystem::exists(p);
      |                   ^~~~~~~~~~
p1_fail_no_include.cpp:6:38: error: 'p' was not declared in this scope
    6 |     bool e = std::filesystem::exists(p);
      |                                      ^

Read the first error only: it names std::filesystem itself, not path and not exists. The namespace was never declared, so the compiler stops at the first component of the qualified name and never gets to judge the rest. The language mode here is already C++17 - the mode is not the problem in this shape, the missing header is. The third error is fallout: because the declaration of p failed, the use of p on the next line has nothing to refer to. Fixing the include removes all three errors at once, which is the usual sign that a cascade of errors has a single cause at the top.

The second failure: <filesystem> included, compiled as C++14

The same program with the header included still fails when the language mode is older than the library:

C++
#include <filesystem>
#include <iostream>

int main() {
    std::filesystem::path p{"data.txt"};
    bool e = std::filesystem::exists(p);
    std::cout << e << "\n";
    return 0;
}
Terminal
$ g++ -std=c++14 p2_fail_cpp14.cpp -o p2_fail_cpp14
p2_fail_cpp14.cpp: In function 'int main()':
p2_fail_cpp14.cpp:5:10: error: 'std::filesystem' has not been declared
    5 |     std::filesystem::path p{"data.txt"};
      |          ^~~~~~~~~~
p2_fail_cpp14.cpp:6:19: error: 'std::filesystem' has not been declared
    6 |     bool e = std::filesystem::exists(p);
      |                   ^~~~~~~~~~
p2_fail_cpp14.cpp:6:38: error: 'p' was not declared in this scope
    6 |     bool e = std::filesystem::exists(p);
      |                                      ^

The diagnostic is identical to the missing-include shape, line for line apart from the file name and the -std flag. That identity is the trap in this shape: the include is visibly present at the top of the file, so the error looks wrong, and the actual fault is in the build command, not the source. In C++14 mode the <filesystem> header is read but declares no std::filesystem names, because the library does not exist in that standard. When the include is present and the error persists, check the -std flag on the compile line - or the language standard setting behind it - before changing any code.

The header sweep: neighboring headers do not substitute

A missing standard type is sometimes rescued by accident, when another header happens to include the one that declares it. That does not happen here. Replacing <filesystem> with <fstream> - the closest neighbor by subject - fails with the same first error:

C++
#include <fstream>
#include <iostream>

int main() {
    std::filesystem::path p{"data.txt"};
    bool e = std::filesystem::exists(p);
    std::cout << e << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 sweep_fstream.cpp -o sweep_fstream
sweep_fstream.cpp: In function 'int main()':
sweep_fstream.cpp:5:10: error: 'std::filesystem' has not been declared
    5 |     std::filesystem::path p{"data.txt"};
      |          ^~~~~~~~~~
sweep_fstream.cpp:6:19: error: 'std::filesystem' has not been declared
    6 |     bool e = std::filesystem::exists(p);
      |                   ^~~~~~~~~~
sweep_fstream.cpp:6:38: error: 'p' was not declared in this scope
    6 |     bool e = std::filesystem::exists(p);
      |                                      ^

The same program was compiled with each of <string>, <vector>, <algorithm>, <system_error> and <cstdio> in place of <filesystem>, at C++17: every one fails with the same first error, 'std::filesystem' has not been declared, and none of the six headers probed - <fstream> above or these five - rescues the program. File streams are not the filesystem library: <fstream> declares stream classes that read and write file contents, and it does not pull in the path and directory declarations. The only header that declares the std::filesystem names — path, exists, and the rest — is <filesystem> itself.

The same fault at namespace scope: "does not name a type"

Move the failing name out of the function - a path declared as a global - and the wording changes:

C++
#include <iostream>

std::filesystem::path p{"data.txt"};

int main() {
    std::cout << p.string() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 p3_fail_global.cpp -o p3_fail_global
p3_fail_global.cpp:3:6: error: 'filesystem' in namespace 'std' does not name a type; did you mean 'system'?
    3 | std::filesystem::path p{"data.txt"};
      |      ^~~~~~~~~~
      |      system
p3_fail_global.cpp: In function 'int main()':
p3_fail_global.cpp:6:18: error: 'p' was not declared in this scope
    6 |     std::cout << p.string() << "\n";
      |                  ^

At namespace scope the name is being read as a type, so the error is phrased as a type error: filesystem in namespace std does not name a type. The "did you mean system" suggestion is the compiler matching the spelling against a namespace that does exist; std::system is a function, not a container for path, and following the suggestion leads nowhere - it is a spelling neighbor, not the fix. The cause and the repair are the same as in the function-scope shape: <filesystem> was never included. When a search for the exact error text lands on either wording, treat them as the same fault and check the same two things - the include, then the language mode.

The fix: include <filesystem> and compile at C++17 or later

Both halves together - the header included, the mode at C++17 - and the library is fully usable. This program creates a directory, writes a five-byte file into it, reports on the file, and removes the directory again:

C++
#include <filesystem>
#include <fstream>
#include <iostream>

int main() {
    std::filesystem::path dir{"probe_dir"};
    std::filesystem::create_directory(dir);
    std::filesystem::path file = dir / "data.txt";
    {
        std::ofstream out{file};
        out << "hello";
    }
    std::cout << "exists " << std::filesystem::exists(file) << "\n";
    std::cout << "size " << std::filesystem::file_size(file) << "\n";
    std::cout << "name " << file.filename().string() << "\n";
    std::filesystem::remove_all(dir);
    std::cout << "after remove " << std::filesystem::exists(file) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 p4_fix.cpp -o p4_fix
$ ./p4_fix
exists 1
size 5
name data.txt
after remove 0

Every name that failed before now resolves: path holds the directory and the file path, the / operator joins them, create_directory, exists, file_size and remove_all are the operations the header page lists, and filename() is the path member that returns the last component. The output confirms each step in order - the file exists, it is the 5 bytes that were written, its filename is data.txt, and after remove_all it no longer exists. Note the division of labor in the includes: <filesystem> provides the paths and the operations on them, while the file's contents are still written through <fstream> - the two headers are complements, not alternatives.

Neighboring error: a member path does not have

Once <filesystem> is included and the mode is right, a wrong member name produces a different error on a different subject. file_size is a free function in std::filesystem that takes a path - it is not a member of path - so calling it as a member fails like this:

C++
#include <filesystem>
#include <iostream>

int main() {
    std::filesystem::path p{"data.txt"};
    std::cout << p.file_size() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 p5_neighbour_no_member.cpp -o p5_neighbour_no_member
p5_neighbour_no_member.cpp: In function 'int main()':
p5_neighbour_no_member.cpp:6:20: error: 'class std::filesystem::__cxx11::path' has no member named 'file_size'
    6 |     std::cout << p.file_size() << "\n";
      |                    ^~~~~~~~~

The contrast separates the two errors cleanly. This error names the class - std::filesystem::__cxx11::path, GCC's internal spelling of std::filesystem::path - and the missing member, which proves the namespace and the class were both found: the include and the mode are already right, and only the call shape is wrong. The title error names std::filesystem itself, because the namespace was never there to be searched. The repair here is to call the free function, std::filesystem::file_size(p), exactly as the fixed program above does.

Neighboring case: a missing file at runtime is not a compile error

With the include and the mode correct, std::filesystem code compiles - and can still fail when it runs, which is a different kind of problem from everything above. Asking for the size of a file that does not exist shows both forms of that runtime failure in one program:

C++
#include <filesystem>
#include <iostream>
#include <system_error>

int main() {
    std::error_code ec;
    auto n = std::filesystem::file_size("missing.txt", ec);
    std::cout << "ec value " << ec.value() << "\n";
    std::cout << "size " << n << "\n";
    try {
        auto m = std::filesystem::file_size("missing.txt");
        std::cout << "size " << m << "\n";
    } catch (const std::filesystem::filesystem_error& e) {
        std::cout << "caught filesystem_error\n";
    }
    return 0;
}
Terminal
$ g++ -std=c++17 p6_neighbour_runtime.cpp -o p6_neighbour_runtime
$ ./p6_neighbour_runtime
ec value 2
size 18446744073709551615
caught filesystem_error

Nothing here is a compile error: the program builds and runs to completion. The overload that takes a std::error_code reports the failure through that code instead of throwing - ec comes back set, with the value 2, and the program carries on. The overload without it throws std::filesystem::filesystem_error, the exception class the header declares for file system errors, and the catch is what lets this program finish normally. Keep the two stages separate when reading a failure: an error naming std::filesystem at compile time is the include or the mode; a filesystem_error from a compiled program is the operation failing on the actual file system, and no include change fixes it.

Decision summary

  • The first error is 'std::filesystem' has not been declared and the file has no <filesystem> include: add #include <filesystem> - every later error in the cascade traces to the missing namespace.
  • The first error is the same but <filesystem> is visibly included: the language mode is older than C++17 - the compile line shows -std=c++14 or an equivalent setting; compile at -std=c++17 or later instead of changing code.
  • The error reads 'filesystem' in namespace 'std' does not name a type, with or without a "did you mean system" suggestion: the same missing namespace, read at namespace scope - apply the same two checks, include first, then mode, and ignore the system suggestion.
  • The program includes some other header that sounds related - <fstream>, <string>, <vector>, <algorithm>, <system_error> or <cstdio>: none of them declares std::filesystem; the probed sweep found no rescue, so add <filesystem> itself.
  • The error instead names the class, as in 'class std::filesystem::__cxx11::path' has no member named 'file_size': the namespace was found and the call shape is wrong - file_size is a free function, called as std::filesystem::file_size(p).
  • The program compiles and then throws std::filesystem::filesystem_error at runtime: the operation failed on the file system itself - handle it with a catch, or call the std::error_code overload, which reports the failure through the code instead of throwing.

Checklist

  • The file includes <filesystem> itself - not a neighboring header in its place - before any std::filesystem:: name is used.
  • The compile line, or the build system's language standard setting, is C++17 or later; an included <filesystem> declares nothing in C++14 mode.
  • The first error is read before the cascade: errors about the variable that failed to declare, or about later uses, disappear when the namespace error at the top is fixed.
  • A "does not name a type" wording and a "has not been declared" wording are treated as the same fault; the "did you mean system" suggestion is not followed.
  • Operations that can fail on real files are written knowing which form they use: the throwing overload, caught as std::filesystem::filesystem_error, or the std::error_code overload, checked after the call.
  • The fixed program compiles at C++17, runs, and its output shows the filesystem operations doing what the code asked - files found, sizes reported, paths decomposed.

Sources

  • cppreference.com, Filesystem library — the library's date and contents: the Filesystem library is since C++17, merged into ISO C++ as of C++17 from ISO/IEC TS 18822:2015, its feature-test macro __cpp_lib_filesystem is 201703L, and its classes and operations - path, exists, file_size, current_path - are all marked (C++17); also the note that GNU implementations before 9.1 require linking with -lstdc++fs, which is why the version of the library matters alongside the language mode.
  • cppreference.com, Standard library header (C++17) — what the header declares: path, filesystem_error, directory_entry, directory_iterator and the filesystem operations, all in namespace std::filesystem and all (C++17), including the paired throwing and std::error_code overloads of file_size that the runtime section separates.
  • cppreference.com, std::filesystem::path — the class at the centre of the error: class path since C++17, an object representing a path on a filesystem, with filename(), parent_path(), stem() and extension() among its decomposition members - and file_size not among them, which is the boundary the neighboring-error section draws.

Keep Exploring