设置了Vertice(Vertex 类)s
后,我想进入. 换句话说,Vertice *address
set<Vertice>::iterator it
address = &(*it);
但是,回报
error: assigning to 'Vertice *' from incompatible type
'const value_type *' (aka 'const Vertice *')
任何想法?
设置了Vertice(Vertex 类)s
后,我想进入. 换句话说,Vertice *address
set<Vertice>::iterator it
address = &(*it);
但是,回报
error: assigning to 'Vertice *' from incompatible type
'const value_type *' (aka 'const Vertice *')
任何想法?
保存的元素std::set
是不可修改的,std::set::iterator
也是 const 迭代器。这意味着&(*it)
您将获得一个指向 const (ie const Vertice*
) 的指针,该指针不能分配给指向 non-const (ie Vertice*
) 的指针。
您可以将类型更改address
为const Vertice*
。
const Vertice* address = &(*it);
中的条目set
不能被外部代码改变(允许它可能违反set
s 不变量)。如果你想要一个直接指针(你不应该),它需要是const Vertice*
(安全的,非变异的),而不是Vertice*
(它可能会改变 中的条目set
)。这就是错误告诉你的。