关于指针和重载的问题(.append() 不能在一行中工作)

发布于 2024-11-26 17:46:43 字数 1106 浏览 1 评论 0原文

在我的程序中,我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灼痛 2024-12-03 17:46:43

fs::path.string() 返回 const&所以你不能在其中添加任何内容,首先你需要制作一个副本,

但你为什么要这样做呢?有一种明显的方法可以附加嵌套路径:

path /nested_pa​​th

编辑:

typedef vector<fs::path> path_vec;
path_vec pv;

for (auto it = pv.begin(), end = pv.end(); it != end; ++it)
    *it /= "prx";

或者创建目录而不是修改向量值,请将最后一行替换为:

fs::create_directory(*it / "prx");

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");
一桥轻雨一伞开 2024-12-03 17:46:43

it->string() 可能是常量。你为什么不说:

const std::string a = it->string() + "/prx";

it->string() is probably constant. Why don't you say:

const std::string a = it->string() + "/prx";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文