question about pointers and overloading (.append() doesn't work in one line)
In my program I would like to manipulate boost::filesystem::path elements of a vector in a for loop.
typedef vector<fs::path> path_vec;
path_vec pv;
for (auto it = pv.cbegin(), end = pv.cend(); it != end; ++it)
What I would like to do is to add a string to the end of the path.
If I do it like this, it works fine:
stringstream image_file_0001;
image_file_0001 << it->string() << "/x_alpha0001.png";
p1 = image_file_0001.str();
If I do it like this, it works fine too:
string a = it->string();
string b = a.append("/prx");
But if I try to do it in one line
string c = it->string().append("/prx");
or
string d = (it->string()).append("/prx");
it gives compile err开发者_开发技巧ors:
7 overloads have no legal conversion for 'this' pointer
I think it must be my lack of knowledge about how to use pointers, or is it something to do with the boost::filesystem::path .string() function?
OK, the thing I would like to do is to create a directory with "/prx"
appended to the origianl path in *it
. Can I do it in one line?
boost::filesystem::createdirectory ( something here );
What seems to be my problem is that I don't understand that why would .append() modify the original string. Isn't it a function which returns an other string, which I can use freely, while just reading the original string?
fs::path.string()
returns const& so you cannot append anything to it, first you need to make a copy
but why do you do this at all? there's a obvious way to append nested path:
path / nested_path
EDIT:
typedef vector<fs::path> path_vec;
path_vec pv;
for (auto it = pv.begin(), end = pv.end(); it != end; ++it)
*it /= "prx";
or to create directories instead of modifying vector values, replace the last line by:
fs::create_directory(*it / "prx");
it->string()
is probably constant. Why don't you say:
const std::string a = it->string() + "/prx";
精彩评论