2012-08-13 119 views
1

我需要幫助。當試圖在一組,我得到以下錯誤迭代:無法迭代設置

Error 1 error C2440: 'initializing' : cannot convert from 'std::_Tree_const_iterator<_Mytree>' to 'compound_objectNS::Compound_object *' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xmemory 208

我得到了下面的代碼:從文件「compound_object.cpp」 提取物:

typedef compound_objectNS::Compound_object OBJECT 
    bool OBJECT::operator== (const Compound_object &object) const 
    { 
     return this == &object; 
    } 

    bool OBJECT::operator< (const Compound_object &object) const 
    { 
     return this->m_numberOfObject < object.m_numberOfObject; 
    } 

這裏,讓集來排序元素我重載運算符 「==」 和 「<」

客戶端文件:

for (objectImitatorNS::set<compound_objectNS::Compound_object*>::iterator it = Objects->begin(); 
     it != Objects->end(); ++it) 
    { 
     this->m_imitatedObjects->insert(it); 
    } 

正如我想出的錯誤是當行

this->m_imitatedObjects->insert(it)

執行。

如何解決這個問題?

回答

4

std::set::insert(帶一個參數的版本)不需要迭代器。它需要一個價值。 See here。您可以嘗試:

this->m_imitatedObjects->insert(*it); 
+0

'insert'確實可以「取一個迭代器」,只是不在OP所尋找的過載中......(例如'insert(it,0)'將會「工作」)。 – 2012-08-13 06:04:24

+0

當然可以。我編輯過,說帶有一個參數的版本不需要迭代器。 – Cornstalks 2012-08-13 06:05:31

+0

謝謝!工作正常 – 2012-08-13 06:12:37

4

this->m_imitatedObjects->insert(*it);。您正在插入

+0

感謝您的回答! – 2012-08-13 06:13:22

0

您必須插入值而不是迭代器obj。