【问题标题】:Error while assigning a pair value to a map key将对值分配给映射键时出错
【发布时间】:2017-07-23 23:03:58
【问题描述】:

这是我的代码的摘录:

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> &)

这是什么意思?我尝试了不同的方法,但仍然得到这个或类似的错误。谢谢!

【问题讨论】:

  • 不要使用m_srcHitData[edgeId] = std::make_pair( hit, std::vector&lt;POINT_3d&gt;() );,而是尝试使用m_srcHitData[edgeId].insert( std::make_pair( hit, std::vector&lt;POINT_3d&gt;() );,看看这是否有效或给你一个不同的编译器错误。嗯,现在我考虑到该插入可能不适用于 m_srcHitDat[idx] 与 einpoklum 所说的相同的问题,因为 constness。
  • @Francis 您实际上并不是指 m_srcHitData[edgeId].insert() ,对吧?它应该是 m_srcHitData.insert ( pair (edgeId, ...) )。这行得通,谢谢!
  • 好吧,当我回答时,我的 IDE 没有打开。所以我试图摆脱记忆......将我的答案作为构建地图的适当方式。

标签: c++ stdmap std-pair


【解决方案1】:

嗯,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>() );

但是,如果您的向量很长,您可能实际上并不想复制所有这些数据。

【讨论】:

    【解决方案2】:

    你代码中的这部分看起来很丑...

    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&lt;POINT_3d&gt;

    #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&lt;T&gt; 的对象替换为 strings

    为了简化演示,我还在structclass 上使用了公共接口。通常class 中的容器将是protectedprivate 具有辅助功能。

    EDIT 这是为了帮助首先构建地图。一旦你的地图工作了,你可以修改它,如果需要,添加const 存储类型,但使用它们可能会很棘手。请参阅 einpoklum 答案中的链接。

    如果您使用的是较新版本的 C++,您可以更改以下代码行:

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

    进入这个:

    auto it = myClass.myMap_.begin();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-05
      • 1970-01-01
      相关资源
      最近更新 更多