引用 vector::front 可以,但 vector::begin 不行

发布于 2024-09-09 00:47:29 字数 849 浏览 7 评论 0原文

我有这段代码:

cerr << client->inventory.getMisc().front()->getName() << endl;
vector<itemPtr>::iterator it;
it = client->inventory.getMisc().begin();
cerr << (*it)->getName() << endl;

让我解释一下:

client 是一个 tr1::shared_ptr ,它指向一个具有名为 inventory< 的成员的对象/code> 具有可通过 getMisc() 访问的私有 vector 成员。 itemPtrtr1::shared_ptr 的类型定义,getName() 返回私有 std::stringItem 的 code> 成员。

本质上,client->inventory.getMisc() 归结为 std::vector,我正在尝试获取其第一个元素的迭代器。

问题是第四行出现段错误。显然它指向的迭代器或shared_ptr是无效的。我使用第一个 cerr 语句来测试向量本身是否有效,并且它按应有的方式打印,所以我认为它是有效的。

我做错了什么吗?或者,你们会做什么来调试这个?

I have this bit of code:

cerr << client->inventory.getMisc().front()->getName() << endl;
vector<itemPtr>::iterator it;
it = client->inventory.getMisc().begin();
cerr << (*it)->getName() << endl;

Let me explain that a bit:

client is a tr1::shared_ptr that points to an object that has a member named inventory that has a private vector<itemPtr> member accessible by getMisc(). itemPtr is a typedef for tr1::shared_ptr<Item>, and getName() returns a private std::string member of Item.

Essentially, client->inventory.getMisc() boils down to a std::vector, and I'm trying to get an iterator to its first element.

The problem is that the fourth line segfaults. Apparently either the iterator or the shared_ptr it points to is invalid. I used the first cerr statement to test if the vector itself was valid, and it prints as it should, so I think it is.

Is there anything I'm doing wrong? Alternatively, what would you guys do to debug this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

绾颜 2024-09-16 00:47:29

getMisc 的签名到底是什么?

如果您实际上返回的是 std::vector,那么您将返回列表的副本。在这种情况下,第一个访问模式将(缓慢地)起作用,因为临时副本直到 front 完成执行之后才会被销毁,此时 itemPtr 本身已被复制变成临时的。第二个失败是因为在使用 begin 获取迭代器后,临时超出范围并被销毁,使刚刚创建的迭代器挂起。

Exactly what is the signature of getMisc?

If you're actually returning a std::vector<itemPtr> then you are returning a copy of the list. In that case, the first access pattern will work (slowly) because the temporary copy doesn't get destroyed until after front finishes executing, by which time the itemPtr itself is copied into a temporary. The second fails because after getting at the iterator with begin, the temporary falls out of scope and is destroyed, leaving the just-created iterator hanging.

你的心境我的脸 2024-09-16 00:47:29

你们会做什么来调试这个?

单步执行代码,看看 front() 返回的内容和 (*i​​t) 返回的内容有什么不同。

what would you guys do to debug this?

Step though the code to see what's different about what front() returns and what (*it) returns.

假扮的天使 2024-09-16 00:47:29

你确定向量不为空吗? frontbegin 的行为可能略有不同,并且 front 纯粹是偶然工作的,而迭代器本身的额外检查会在以下情况下导致 seg 错误:它被使用了。

Are you sure that the vector isn't empty? It's possible that front and begin behave slightly differently and front could work by pure chance, while additional checks in the iterator itself cause the seg fault when it's used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文