2012-05-17 80 views
1

我需要複製包含指向類的std :: vector。該功能是:迭代指針的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(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構造函數沒有正確接收數據。

你能幫我嗎?

在此先感謝。

+0

爲什麼你需要存儲指向'Derived'的指針?您可以存儲'Derived'對象,並說'm_pDerivate = source.m_pDerivate' –

+0

此外,在一個構造函數中,m_pDerivate(我假設它是一個類成員)將剛剛構建,因此不需要清除。 –

回答

8

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

  • *it的類型是Derivate*
  • **it是類型的Derivate

變化:

m_pDerivate.push_back(new Derivate(it)); 

到:

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

它的工作原理!謝謝。 –