0

I ran into this issue when creating a boost::filesystem::path object (boost v1.55). I couldn't figure out how to create a path from from a String variable, or concatenation of Strings?

//Example 1
namespace fs = boost::filesystem;
String dest = "C:/Users/username";  
fs::path destination (dest); //Error here

//Example 2
namespace fs = boost::filesystem;
String user = "username";
fs::path destination ("C:/Users/" + user); //Error here as well.

//Example 3
namespace fs = boost::filesystem;
fs::path destination ("C:/Users/username");

I've only been able to create a path object when the entire string is specified between double quotes like example 3 above, but this does not allow for a variable input.

Basically, how would I implement the fs::path object class using a String as my starting point?

Thanks for any assistance!

edit

Link to boost/filesystem path documentation. Relearning c++, so a some of it is still a bit over my head... I don't quite understand how the constructor works here... and really don't know what to ask at this point.... I'd definitely appreciate any pointers.

RisaAudr
  • 53
  • 1
  • 5
  • 10
  • 2
    Well, what is `String`? How can you turn it into something that the documentation says a `path` can be constructed from? You have the definition of `String`, you tell us. Probably ask for a C-string or `std::string` from it or something. – GManNickG Feb 15 '17 at 21:49
  • I'm sort of relearning c++... I don't exactly remember/know the 'definition' of `String` in the way that you are asking. Updated to include links to documentation... – RisaAudr Feb 15 '17 at 21:55
  • 1
    Nobody but you knows what `String` is, please post a [MCVE](http://stackoverflow.com/help/mcve). – GManNickG Feb 15 '17 at 22:04
  • 1
    Apparently no-one is willing to elaborate, so let me: `String` is not a standard C++ class. There is a class called `string` (actually, `std::string`), beginning with a lower-case "s". It looks like your program contains another class which is called `String` (upper case "S"). The constructor isn't working because you're passing it one of these `String` objects which it knows nothing about; what you need to give it is a `std::string`. – davmac Feb 15 '17 at 23:55
  • @RisaAudr To clarify, `String` is part of *your code*, not part of the C++ language. You should be able to find the definition somewhere in your codebase. – Kyle Strand Feb 15 '17 at 23:56
  • Thanks all! That managed to clarify what exactly was happening. I'm using c++ builder, which has `String` defined as a `const char*` (I think). Regardless that led me down the right tracks. I posted my solution as an answer in case anyone else runs across builder's weird `String` vs `std::string` issues. Again, thanks for the help & clarification :) – RisaAudr Feb 16 '17 at 17:12

1 Answers1

0

Thanks GManNickG - you actually managed to solve my issue. I'm using C++ builder 10.1, and was able to mess with String for a while, assigning it values, etc. It was actually the ShowMessage() method that led me to my answer - in c++ builder, it wants an AnsiString argument to work, and a std::string wouldn't compile. C++ Builder 10.1 defines String to be an AnsiString, not std::string. Again, I'm newish to c++ so when using namespace std I didn't realize the difference (most of my prior knowledge of Obj Oriented is from java where you define a string as String.

//Working Example in C++ Builder 10.1 Starter
namespace fs = boost::filesystem;
std::string un = "/username";
std::string dest = "C:/Users" + un;  //concatenation test
fs::path destination (dest); //Works, no compiler error now

std::string pathStdString = destination.string(); //retrieve 'dest' as std:string from path
String pathAnsiString = pathStdString.c_str(); //Converts std::string to ansi

ShowMessage(pathAnsiString); //Output box showing the path (valid in C++ Builder)

Hopefully this helps someone else coming along with a similar issue. Also, link to how std::converts to Ansi in case anyone finds it useful.

RisaAudr
  • 53
  • 1
  • 5
  • 10