我正在寻找代码中的错误,但遇到了一个问题:
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/