0

I'm relatively inexperienced with C++ and I'm currently creating a simple Windows Forms project. The following code is providing a strange error that I just can't wrap my head around.

if (folderBrowserDialog1->ShowDialog() == ::DialogResult::OK) {
        String filepath = folderBrowserDialog1->SelectedPath;
        for (auto& p : std::filesystem::directory_iterator(filepath)) {
            // read files from filepath
        }
    }

The error, "no suitable user-defined conversion from System::String ^ to System::String exists" is referring to the folderBrowserDialog1->SelectedPath line. Despite the docs I've checked stating that SelectedPath is a string, apparently it's not the same type of string as C++ is expecting? I assume I need to do some kind of conversion but I'm at a total loss as to how I should do that as I really assumed that SelectedPath would be a regular String.

273K
  • 29,503
  • 10
  • 41
  • 64
  • 1
    `System::String ^` is not valid C++. Are you talking about C++CX or something else? – Nicol Bolas Feb 29 '20 at 22:33
  • I'm honestly not too certain. I'm using a C++/CLR Winforms extension that I got for Visual Studio Community, if that affects anything. – Ryan Dennis Feb 29 '20 at 22:37
  • [It's a pointer to a CLI-managed object](https://stackoverflow.com/questions/202463/what-does-the-caret-mean-in-c-cli). Also, `System::String` is a Microsoft type, but I think `directory_iterator` only accepts `std::string`s (a C++ type), so this code wouldn't work even if you dereferenced the pointer or whatever. You need to find a replacement for `directory_iterator` (good idea) or find a conversion function (worse idea). – HTNW Feb 29 '20 at 22:41
  • The code is just missing the ^ hat, required for reference types. Fix with String^ filepath =...; – Hans Passant Mar 01 '20 at 14:46

0 Answers0