迭代std ::指针向量时出错

时间:2012-05-17 11:10:05

标签: c++ visual-c++

我需要复制一个包含指向类的指针的std :: vector。功能是:

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));
    }
}

Derivate构造函数是:

Derivate::Derivate( const Derivate &source )
{
    _y = source._y; 
    _m = _strdup(source._m);
}

但是当我编译时,我收到以下错误......

 cannot convert parameter 1 from 'std::_Vector_const_iterator<_Myvec>' to 'const Derivate &'

......在第一行:

m_pDerivate.push_back(new Derivate(it));

如果我改变了......

m_pDerivate.push_back(new Derivate((const Derivate &)(*it)));

...编译正常,但Derivate构造函数没有正确接收数据。

你能帮助我吗?

提前致谢。

1 个答案:

答案 0 :(得分:8)

您需要取消引用迭代器和指针:

  • *it的类型为Derivate*
  • **it的类型为Derivate

变化:

m_pDerivate.push_back(new Derivate(it));

为:

m_pDerivate.push_back(new Derivate(**it));