简体   繁体   English

Unordered_map(C ++)中的“错误:分配只读位置”

[英]“error: assignment of read-only location” in unordered_map (C++)

I have an awkward hash table (specifically, an unordered_map) with int keys and vector< vector< int >> data. 我有一个笨拙的哈希表(特别是unordered_map),其中包含int键和vector< vector< int >>数据。 I periodically need to update elements in this two-dimensional vector of ints. 我定期需要更新此int的二维向量中的元素。 There's no intrinsic reason I shouldn't be able to, right? 没有内在的原因我不应该对吧? A newer g++ compiler I've switched to complains of an assignment of read-only location on the line designated below. 我改用了一种较新的g ++编译器,抱怨在下面指定的行上分配了只读位置。

typedef std::tr1::unordered_map< int, vector< vector< int > > > pimap;

vector< Strain * > liveStrains;
pimap phenotypeIs;
int NUM_DEMES = 3;

...
vector< Strain * >::const_iterator lsItr;
for ( lsItr = liveStrains.begin(); lsItr != liveStrains.end(); ++lsItr ) {
 int thisP = (*lsItr)->getPhenotype();
 pimap::iterator piItr = phenotypeIs.begin();
 piItr = phenotypeIs.find( thisP );
 if ( piItr != phenotypeIs.end() ) {
   for ( int d = 0; d < NUM_DEMES; d++ ) {
      ( piItr -> second )[ thisStep ].at( d ) = (*lsItr)->getI( d );  // error here
   }
 }
}

I'm new to C++, so nothing's too obvious. 我是C ++的新手,所以没有什么太明显的。 Thank you for any help. 感谢您的任何帮助。


Following Tim's suggestion 遵循蒂姆的建议

I've replaced the relevant parts of code above with the following: 我已将以下代码的相关部分替换为以下内容:

  pimap::iterator piItr = phenotypeIs.find( thisP );
  if ( piItr != phenotypeIs.end() ) {
    for ( int d = 0; d < NUM_DEMES; d++ ) {
      vector< vector< int > > & thisVec2 = piItr->second;
      vector<int> & thisVec = thisVec2.at( thisStep );
      int & ii = thisVec.at( d );
      ii = (*lsItr)->getI( d );
      // ( piItr -> second )[ thisStep ].at( d ) = (*lsItr)->getI( d ); // error was here
    }

This code compiles without the error and appears to run fine. 该代码可以编译而不会出现错误,并且可以正常运行。 Like Tim, I still don't quite understand why the fix works. 像蒂姆一样,我仍然不太明白为什么该修复程序有效。 The error was previously appearing with gcc version 4.1.2 20080704 (Red Hat 4.1.2-44) but not with gcc version 4.0.1 (Apple Inc. build 5465). 该错误先前在gcc版本4.1.2 20080704(Red Hat 4.1.2-44)中出现,但在gcc版本4.0.1(Apple Inc.内部版本5465)中没有出现。 I will try to dissect the error more carefully when I'm not under a tight deadline! 如果我没有紧迫的期限,我将尝试更仔细地分析错误!

Are you sure there really are thisStep + 1 elements in every first-level vector and NUM_DEMES elements in every second-level vector? 你确定真的有thisStep + 1中的每一级的矢量元素和NUM_DEMES元素在每一个二级矢量?

You're not actually assigning to a map iterator, if I read correctly, so I suspect the error is in the vector access. 如果我正确阅读,实际上并没有分配给地图迭代器,因此我怀疑错误在于矢量访问。

It may be helpful to break that last statement up into multiple statements so that only one thing is being done on each to narrow down where the problem is. 将最后一个语句分解为多个语句可能会有所帮助,以便对每个语句仅执行一项操作以缩小问题所在。 eg, 例如,

Strain* strain = *lsItr;
vector<vector<int> >& vv = piItr->second;
vector<int>& v = vv[thisStep];
int& i = v.at(d);     // <-- My bet is that the error occurs here or the prev. line
i = strain->getI( d );

By the way, piItr = phenotypeIs.begin(); 顺便说一句, piItr = phenotypeIs.begin(); has no effect here, it could be simply: 在这里没有影响,可能很简单:

pimap::iterator piItr = phenotypeIs.find( thisP );
( piItr -> second )[ thisStep ].at( d )

at() returns an iterator into the inner vector, not access to the value. at()将迭代器返回到内部向量中,而不访问该值。 What you want is 你想要的是

 *(( piItr -> second )[ thisStep ].at( d ))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM