std::filesystem C++17 ?

hello everybody. i'm trying to use C++17 filesystem library to open all files in a directory.

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
#include <filesystem>
int main(){
    for (auto &f : std::filesystem::directory_iterator("Test")){
        std::cout << f;
        std::string s = f.string();
    }
return 0;
}


for some reason, the first line inside the loop works, and the paths are printed. but the second one give me this error:
error: ‘const class std::filesystem::__cxx11::directory_entry’ has no member named ‘string’


i'm using:
Debian testing (buster) 64
Code::Blocks 16.01
g++ 8.1.0
and these options to compile: -std=c++17 stdc++fs
Last edited on
Here's the page detailing objects of type filesystem::__cxx11::directory_entry

https://en.cppreference.com/w/cpp/filesystem/directory_entry

As you can see, you're trying to call a class function that does not exist. You can't do this. You can only call functions that do exist.
The iterator iterates over objects of type std::filesystem::directory_entry

1
2
3
4
for( const auto& dir_entry : std::filesystem::directory_iterator("Test") ) {

    const std::string s = dir_entry.path().string();
}

oops, i understand now.
i didn't realize the string method was inside the path class. thanks everyone for the help!
Topic archived. No new replies allowed.