2014-09-03 97 views
0

當試圖編譯這個方法時,我從gcc中得到了很多錯誤。 zones_是std ::向量const指針const對象不能編譯

std::map<int,std::vector<Zone const * const>> 

這是MyClass的私人成員。

//get unique zones 
std::vector<Zone const* const> MyClass::getZones() const { 
    std::vector<Zone const * const> zones; //why can I not do this??? 
    std::map<Zone const * const,int> zone_set; 
    for(auto & pair : zones_) { 
     for(Zone const * const z : pair.second) { 
      if(zone_set.count(z) == 0) { 
      zone_set[z] = 1; 
      zones.push_back(z); //cannot do this 
      } 
     } 
    } 
return zones; 
} 

我可以有一個const指針的向量指向常量對象嗎?

+0

你可以發佈錯誤嗎?我很好奇他們 – 2014-09-03 13:56:33

回答

3

否。通常,矢量的元素不能爲const,因爲當矢量數組需要重新分配時,它們必須移動。你必須存儲Zone const *;或者可能使用不同的容器類型(listdeque或可能set),如果你確實需要不變的元素。