[英]Error iterating a std::vector of pointers
I need to copy a std::vector that containing pointers to a class. 我需要复制包含指向类的指针的std :: vector。 The function is:
该函数是:
Clone::Clone( const Clone &source )
{
m_pDerivate.clear();
std::vector<Derivate *>::const_iterator it;
it = source.m_pDerivate.begin();
for (it = source.m_pDerivate.begin(); it != source.m_pDerivate.end(); ++it) {
m_pDerivate.push_back(new Derivate(it));
}
}
And the Derivate constructor is: 派生构造函数是:
Derivate::Derivate( const Derivate &source )
{
_y = source._y;
_m = _strdup(source._m);
}
But when I compile, I get the following error ... 但是当我编译时,出现以下错误...
cannot convert parameter 1 from 'std::_Vector_const_iterator<_Myvec>' to 'const Derivate &'
... in the line: ...在一行中:
m_pDerivate.push_back(new Derivate(it));
If I change the line by ... 如果我改变线...
m_pDerivate.push_back(new Derivate((const Derivate &)(*it)));
... compile fine but the Derivate constructor not receives the data correctly. ...可以正常编译,但是Derivate构造函数无法正确接收数据。
Can you help me? 你能帮助我吗?
Thanks in advance. 提前致谢。
You need to dereference the iterator and the pointer: 您需要取消引用迭代器和指针:
*it
is of type Derivate*
*it
是Derivate*
类型Derivate*
**it
is of type Derivate
**it
是Derivate
类型 Change: 更改:
m_pDerivate.push_back(new Derivate(it));
to: 至:
m_pDerivate.push_back(new Derivate(**it));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.