gpt4 book ai didi

c++ - 指针是否存储在 std::set const 中?

转载 作者:太空宇宙 更新时间:2023-11-04 14:38:11 25 4
gpt4 key购买 nike

我正在寻找代码中的错误,但遇到了一个问题:

 class a
{
public:
void foo(int a) {}
}

std::set<a*> set;
std::set<a*>::iterator it = set.begin();

it->foo(55); //gives me error:
// error: request for member ‘foo’ in ‘* it.std::_Rb_tree_const_iterator<_Tp>::operator-><a*>()’, which is of pointer type ‘a* const’ (maybe you meant to use ‘->’ ?)

为什么它不允许我在上面使用非常量函数?如何在不使用强制转换的情况下拥有一组非常量指针?

最佳答案

您需要取消引用两次:

(*it)->foo(55);

it是指针的迭代器。如果您有 std::set<a>,您的代码将是正确的而不是 std::set<a*> .

关于c++ - 指针是否存储在 std::set const 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14606637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com