2013-02-22 8 views
5

Biorąc pod uwagę kod C++ w pierwszym fragmencie kodu poniżej, otrzymuję błędy kompilacji wskazane w drugim fragmencie kodu. Wygląda na to, że robię źle podczas przechodzenia przez instancję wektorową. Czy możesz mi powiedzieć, jak mogę przezwyciężyć te problemy z kompilacją? Dzięki. Linia 171 jest oznaczona w kodzie.Wektor Iterator: brak dopasowania dla 'operator ='

SNIPPET 1 (Code)

#include <string> 
#include <vector> 
#include <iterator> 

class VipAddressSetEntity : BaseEntity 
{ 
public: 
     VipAddressSetEntity() : BaseEntity() { } 
     VipAddressSetEntity(std::string &uuid, std::string &name) : BaseEntity(uuid, name) { } 

     VipAddressSetEntity(const VipAddressSetEntity &copyin) 
     { 
     setUUID(copyin.getUUID()); 
     setName(copyin.getName()); 

     std::vector<VipAddressEntity>::iterator iter; 
       /* LINE 171 is the following*/ 
     for(iter = copyin.mVipAddressList.begin(); iter != copyin.mVipAddressList.end(); iter++) 
     { 
       addVipAddress(*iter); 
     } 
     } 

     VipAddressSetEntity operator = (const VipAddressSetEntity &rhs) 
     { 
     setUUID(rhs.getUUID()); 
     setName(rhs.getName()); 

     std::vector<VipAddressEntity>::iterator iter; 
     for(iter = rhs.mVipAddressList.begin(); iter != rhs.mVipAddressList.end(); iter++) 
     { 
       addVipAddress(*iter); 
     } 

     return *this; 
     } 

     void addVipAddress(VipAddressEntity &entity) 
     { 
     mVipAddressList.push_back(entity); 
      } 

     VipAddressEntity & getVipAddress(std::string uuid) 
     { 
     std::vector<VipAddressEntity>::iterator iter; 
     for(iter = mVipAddressList.begin(); iter != mVipAddressList.end(); iter++) 
     { 
      if(iter->getUUID() == uuid) 
      { 
       return *iter; 
      } 
     } 

      return NULL; 
     } 


     const std::vector<VipAddressEntity>& getVipAddressList() const { return mVipAddressList; } 


private: 
    std::vector<VipAddressEntity> mVipAddressList; 

}; 

SNIPPET 2 (wyjście Compilation)

Entity.hpp: In copy constructor ‘ECLBCP::VipAddressSetEntity::VipAddressSetEntity(const ECLBCP::VipAddressSetEntity&)’: 
Entity.hpp:171:44: error: no match for ‘operator=’ in ‘iter = copyin.ECLBCP::VipAddressSetEntity::mVipAddressList.std::vector<_Tp, _Alloc>::begin<ECLBCP::VipAddressEntity, std::allocator<ECLBCP::VipAddressEntity> >()’ 
Entity.hpp:171:44: note: candidates are: 
In file included from /usr/include/c++/4.7/bits/stl_algobase.h:68:0, 
        from /usr/include/c++/4.7/bits/char_traits.h:41, 
        from /usr/include/c++/4.7/string:42, 
        from Entity.hpp:11: 
/usr/include/c++/4.7/bits/stl_iterator.h:710:11: note: __gnu_cxx::__normal_iterator<ECLBCP::VipAddressEntity*, std::vector<ECLBCP::VipAddressEntity> >& __gnu_cxx::__normal_iterator<ECLBCP::VipAddressEntity*, std::vector<ECLBCP::VipAddressEntity> >::operator=(const __gnu_cxx::__normal_iterator<ECLBCP::VipAddressEntity*, std::vector<ECLBCP::VipAddressEntity> >&) 
/usr/include/c++/4.7/bits/stl_iterator.h:710:11: note: no known conversion for  argument 1 from ‘std::vector<ECLBCP::VipAddressEntity>::const_iterator {aka __gnu_cxx::__normal_iterator<const ECLBCP::VipAddressEntity*, std::vector<ECLBCP::VipAddressEntity> >}’ to ‘const __gnu_cxx::__normal_iterator<ECLBCP::VipAddressEntity*, std::vector<ECLBCP::VipAddressEntity> >&’ 

Odpowiedz

6

copyin to const VipAddressSetEntity, a więc jest również const. Wywołanie begin na const std::vector<VipAddressEntity> da ci niezmienny iterator typu std::vector<VipAddressEntity>::const_iterator. Wystarczy upewnić się, że iter jest tego typu, a także będzie można przypisać do niego dobrze:

std::vector<VipAddressEntity>::const_iterator iter; 
2

Na tej linii:

for(iter = copyin.mVipAddressList.begin(); iter != copyin.mVipAddressList.end(); iter++) 

Nie można przypisać std::vector<VipAddressEntity>::iterator od copyin.mVipAddressList.begin() ponieważ copyin jest const. Zamiast tego należy użyć numeru const_iterator.

2

copyin jest const odniesienia. Powinieneś użyć const_iterator do iteracji.

Powiązane problemy