2017-07-23 80 views
0

這是我的代碼摘錄:錯誤而分配對值映射關鍵

std::map<int, std::pair< const int, const std::vector<POINT_3d> > > m_srcHitData; 
void addHit(const int edgeId, const int hit) 
{ 
    m_srcHitData[edgeId] = std::make_pair(hit, std::vector<POINT_3d>()); 
} 

我不斷收到錯誤:

stl_pair.h(180): error: no operator "=" matches these operands 
       operand types are: const std::vector<POINT_3d, std::allocator<POINT_3d>> = const std::vector<POINT_3d, std::allocator<POINT_3d>> 
       second = __p.second; 
        ^
    detected during instantiation of "std::pair<_T1, _T2> &std::pair<_T1, _T2>::operator=(const std::pair<_U1, _U2> &) 

這是什麼意思?我嘗試了不同的方法,但仍然得到這個或類似的錯誤。謝謝!

+2

代替使用'm_srcHitData [EdgeID的] = STD的:: make_pair(hit,std :: vector ());'嘗試使用'm_srcHitData [edgeId] .insert(std :: make_pair(hit,std :: vector ());'看看這個工作還是給你一個不同的編譯錯誤。嗯,現在我想這個插入可能無法在'm_srcHitDat [idx]'同樣的問題上說因爲常量所說的einpoklum。 –

+1

@Francis你實際上並不是指m_srcHitData [edgeId] .insert(),對吧?它應該是m_srcHitData.insert(pair(edgeId,...))。這工作,謝謝! –

+0

當我回答時,我沒有打開IDE。所以我試圖離開記憶......參考我的答案作爲構建地圖的適當方式。 –

回答

2

那麼,m_srcHitData[edgeId]是一對const矢量成員。你不能簡單地分配給它,因爲這意味着分配給const向量,這是不可能的......

至於你可以做什麼,請參閱:

How to create a std::map of constant values which is still accessible by the [] operator?

由於@FrancisCugler表明,這可能是,例如,寫作:

m_srcHitData[edgeId].insert(std::make_pair(hit, std::vector<POINT_3d>()); 

但是,如果你的載體很長,你可能不真的想圍繞複製所有數據。

+0

@ M.M:你說得對。 – einpoklum

1

這部分代碼中的那種長相醜陋...

std::map<int, std::pair< const int, const std::vector<POINT_3d> > > m_srcHitData; 

你可以嘗試重組你的一些代碼。

struct Pair { 
    unsigned int key_; 
    std::vector<POINT_3d> points_; 

    Pair() {} // Empty Default 
    Pair(const unsigned int& key, const std::vector<POINT_3d>& points) : 
     key_(key), 
     points_(points) 
    {} 
}; 

則...

std::map<unsigned, Pair> m_srcHitData; 

void addHit(const int edgeId, const int hit) { 
    m_srcHitData[edgeId] = Pair(hit, std::vector<POINT_3d>()); 
} 

我只是我用strings代替你std::vector<POINT_3d>

#include <string> 
#include <iostream> 
#include <map> 

struct Pair { 
    unsigned key_; 
    std::string value_; 

    Pair() {} 
    Pair(const unsigned int& key, const std::string& value) : 
     key_(key), 
     value_(value) {} 
}; 

class MyClass { 
public: 
    std::map<unsigned, Pair> myMap_; 

    void addValue(const unsigned int& key, const std::string& value) { 
     myMap_[key] = Pair(key, value); 
    } 
}; 

int main() { 

    MyClass myClass; 
    myClass.addValue(1, "Hello"); 
    myClass.addValue(2, "World"); 

    typedef std::map<unsigned, Pair>::iterator Iter; 
    Iter it = myClass.myMap_.begin(); 

    for (; it != myClass.myMap_.end(); ++it) { 
     std::cout << "Key: " << it->first << " Pair-Key: " << it->second.key_ << " Pair-value: " << it->second.value_ << std::endl; 
    } 


    std::cout << "\nPress any key and enter to quit." << std::endl; 
    char c; 
    std::cin >> c; 
} 

您可以用上面做這短短的程序來模擬一個類似的結構,但將vector<T>的對象替換爲strings除外。

爲了簡化演示,我還在structclass上使用了公共接口。正常情況下,class中的容器可能是protectedprivate附帶功能。

編輯這是爲了幫助先構建地圖。一旦你有地圖工作,那麼你可以修改它,以在需要時添加const存儲類型,但它們可能會很棘手。請參閱einpoklum答案中的鏈接。

如果使用C的更新版本工作++可以更改這些代碼行:

typedef std::map<unsigned, Pair>::iterator Iter; 
Iter it = myClass.myMap_.begin(); 

成這樣:

auto it = myClass.myMap_.begin();