-3

I keep getting this error when compiling my C++ code. I have included all the headers and have been trouble shooting this code in VS code but with no luck. Any help much appreciated!

ConsoleApplication1.cpp:151:17: error: use of undeclared identifier 'experimental'
        for (auto& p : experimental::filesystem::recursive_directory_iterator(path)) {

Any help on why I am getting the error "use of undeclared identifier 'experimental" would be much appreciated. I have the newest version of vs code installed on my mac and I am just doing g++ ConsoleAppliation1.cpp, and that is where the 4 warnings and 1 error show up. Here are the headers I am including


#include <iostream>
#include "stdafx.h"
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>  
#include <sstream>
#include <map>  
#include <thread>
#include <chrono>
#include <cstdlib>
#include "CSVRow.h"
#include "Definements.h"
#include "Node.h"
#include "Reverse.h"
#include "dijkstras.h"
#include "dijkstras2.h"
#include "smart_ptr.h"
#include "targetver.h"
//#include <Windows.h> 
//#include <Wininet.h>
#include <experimental/filesystem>
//#include <tchar.h>
//#include <urlmon.h>
#include <ctime>
#include <unordered_map>
#include <utility>
#include "ConsoleApplication1.h"
#include <unordered_set>

Just a follow up, thanks to people who have commented I am now getting an

error: no member named 'experimental' in namespace 'std'

when doing

#ifdef __cpp_lib_filesystem
#  include <filesystem>
    namespace fs = std::filesystem;
#else
#  include <experimental/filesystem>
    namespace fs = std::experimental::filesystem;

#endif

Does anyone know how to fix this error of not finding experimental in std when compiling? I am using the newest version of c++ on a mac, using VS code as my IDE

marcelomo
  • 161
  • 2
  • 14

1 Answers1

0

Replace #include <experimental/filesystem> by this

#include <filesystem>
namespace fs = std::filesystem;

And use fs in the rest of your code. E.g. fs::directory_iterator

m88
  • 1,968
  • 6
  • 14
  • Am I suppose to write this in the top of my c++ file, eg. on top of ConsoleApplication1.cpp? If so I get an invalid preprocessing directive for namespace fs = std::experimental::filesystem ? – marcelomo Mar 01 '21 at 15:33
  • Put it on top of the .cpp file(s) where `fs` is used. – m88 Mar 01 '21 at 15:36
  • Typo: should be `ifdef` instead of `if` on 1st line – m88 Mar 01 '21 at 15:38
  • I get an invalid preprocessing directive for namespace when I put it at the top – marcelomo Mar 01 '21 at 15:41
  • sorry, no `#` for the namespaces – m88 Mar 01 '21 at 15:42
  • thank you so much for all the help already! now i am getting an error 'error: no member named 'experimental' in namespace 'std'' , I am using the newest version of c++ so not sur how to fix this – marcelomo Mar 01 '21 at 15:44
  • Honestly if your compiler is up-to-date, just keep `#include ` and `namespace fs = std::filesystem;` – m88 Mar 01 '21 at 15:50
  • Otherwise, maintaining compatibility with old `experimental/filesystem` implementations can be quite the conundrum: https://stackoverflow.com/questions/53365538/how-to-determine-whether-to-use-filesystem-or-experimental-filesystem – m88 Mar 01 '21 at 15:56