关于指针和重载的问题(.append() 不能在一行中工作)
在我的程序中,我想在 for 循环中操作向量的 boost::filesystem::path 元素。
typedef vector<fs::path> path_vec;
path_vec pv;
for (auto it = pv.cbegin(), end = pv.cend(); it != end; ++it)
我想做的是将一个字符串添加到路径的末尾。
如果我这样做,它工作得很好:
stringstream image_file_0001;
image_file_0001 << it->string() << "/x_alpha0001.png";
p1 = image_file_0001.str();
如果我这样做,它也工作得很好:
string a = it->string();
string b = a.append("/prx");
但是如果我尝试在一行中完成它
string c = it->string().append("/prx");
,或者
string d = (it->string()).append("/prx");
它会给出编译错误:
7 个重载对于“this”指针没有合法的转换
我认为这一定是我缺乏如何使用指针的知识,或者与 boost::filesystem::path .string() 函数有关?
好的,我想做的是创建一个目录,并将 "/prx"
附加到 *it
中的原始路径。 我可以在一行中完成它吗?
boost::filesystem::createdirectory ( something here );
我的问题似乎是我不明白为什么 .append() 会修改原始字符串。它不是一个返回另一个字符串的函数,我可以在读取原始字符串的同时自由使用它吗?
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 errors:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
fs::path.string()
返回 const&所以你不能在其中添加任何内容,首先你需要制作一个副本,但你为什么要这样做呢?有一种明显的方法可以附加嵌套路径:
path /nested_path
编辑:
或者创建目录而不是修改向量值,请将最后一行替换为:
fs::path.string()
returns const& so you cannot append anything to it, first you need to make a copybut why do you do this at all? there's a obvious way to append nested path:
path / nested_path
EDIT:
or to create directories instead of modifying vector values, replace the last line by:
it->string()
可能是常量。你为什么不说:it->string()
is probably constant. Why don't you say: