3

I am using Microsoft Visual Studio 2019, and whenever I type std::filesystem there's a red bar under "filesystem", with the messages

namespace "std" has no member "filesystem"

and

name followed by '::' must be a class or namespace name

when I don't have "::" in front of it and when I do respectively.

I have included filesystem so I don't see why it doesn't work.

I hope I provided enough information, if not just ask in the comments. if you can't help me with that, maybe you could help me find an alternative to std::filesystem::recursive_directory_iterator.

Itay123
  • 61
  • 1
  • 6
  • I can't see your code or a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – Stefan Riedel Jun 10 '21 at 15:02
  • 1
    Does this answer your question? [How to enable C++17 compiling in Visual Studio?](https://stackoverflow.com/questions/41308933/how-to-enable-c17-compiling-in-visual-studio) – Hans Olsson Jun 10 '21 at 15:32

1 Answers1

6

The filesystem library was added in C++17 and VS2019 starts in C++14 mode by default.

  • Open Project\<project name> Properties
  • Select Configuration Properties\General
  • To the right, there is a field named C++ Standard, select one of these:
    • ISO C++17 Standard (/std:c++17)
    • Preview - Features from the latest C++ working draft (/std:c++latest)

Then #include <filesystem> and you should be good to go.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • I get an error saying `Element has an invalid value of "/std:c++latest".` – Itay123 Jun 10 '21 at 20:20
  • 1
    @Itay123 Did you change it via the menu as in the answer or in some other way? It has a drop down menu. Don't type the value in manually. What happens if you select C++17 in the drop down menu instead? Is you source code file named _something_ `.c` or _something_ `.cpp`? – Ted Lyngmo Jun 10 '21 at 20:22
  • oh yeah I didn't see the dropdown menu the first time. thanks – Itay123 Jun 10 '21 at 20:56
  • @TedLyngomo yup yup – Itay123 Jun 10 '21 at 20:58
  • @TedLyngomo actually I still have an error saying `Element has an invalid value of "/std:c17".` and one saying `Invalid parameters passed to the Microsoft.Build.CPPTasks.CL task.` – Itay123 Jun 10 '21 at 21:13
  • 1
    @Itay123 Did you change the C language standard too (It's below the C++ standard)? If you select both using the menu drop down - it _should_ work. Otherwise, create a new clean project and try selecting the C++ standard there and test to compile. You just have to put `int main() {}` in the program to test if that works. – Ted Lyngmo Jun 10 '21 at 21:22
  • 1
    didn't work. made a new project like you said and that fixed it. thank you so much! – Itay123 Jun 11 '21 at 07:53
  • 1
    of course! I forgot. thanks for reminding me – Itay123 Jun 11 '21 at 09:32
  • For Visual Studio 2017 users: It's `Configuration Properties \ C/C++ \ Language` and then `C++ Language Standard`. – starriet Aug 16 '22 at 12:18