2015-05-30 26 views
0
pair<CDrug, pair<unsigned,double>> expirednull(pair<CDrug, 
pair<unsigned,double>> temp){ 
    if (temp.first.isValid() == false) 
     temp.second.first = 0; 
    return temp; 
} 

string checkForExpiredDrugs() { 
    stringstream s; 
    vector<CDealer>::iterator it1; 
    map<CDrug, pair<unsigned, double>> d; 
    map<CDrug, pair<unsigned, double>>::iterator it2; 
    //transform algorithm 
    for (it1 = this->m_dealers.begin(); it1 != this->m_dealers.end(); it1++) { 
     s << "Dealer: " << it1->getCompany() << " " << it1->getRepresentative() << " " << it1->getTelephone() << endl; 
     d = it1->getDrugs(); 
     transform(d.begin(),d.end(),d.begin(),expirednull); 
     for (it2 = d.begin(); it2 != d.end(); it2++) { 
      if (it2->first.isValid() == false) { 
       it2->second.first = 0; 
       s << "Expired: " << it2->first << endl; 
      } 
     } 
     it1->setDrugs(d); 
    } 
    return s.str(); 
} 

每當我運行它,它給了我下面的錯誤 - >變換算法給出了「二進制‘=’沒有運營商這需要左邊的操作數。」

錯誤7錯誤C2678:二進制「=」:沒有操作員發現這需要類型的左邊的操作數「const的CDrug」(或沒有可接受的轉化率)

+0

我以爲這是一個*編譯*錯誤,而不是一個* *運行時錯誤。它指的是哪一行? –

回答

2

這是因爲地圖要素是真的: pair< const CDrug, ... >pair< CDrug, ... >

它們的鍵類型是常量,因爲更改映射的現有元素中的鍵會導致麻煩。 (這會使元素未排序,從而打破一些映射不變)。

因此,由轉換函數返回的對象不能分配給map元素=>編譯失敗。

此外,您不能在地圖上使用轉換,因爲您無法分配給地圖迭代器(因爲該鍵爲常量)。 所以,你應該用的for_each代替,在這裏一個相關的問題描述:how to apply transform to a stl map in c++

喜歡的東西:

void expirednull(pair<const CDrug, pair<unsigned,double> > & temp) 
{ 
    if(temp.first.isValid == false) 
     temp.second.first = 0; 
} 

map< CDrug, pair<unsigned,double> > d; 
for_each(d.begin(),d.end(),expirednull); 
+0

是的,我明白,但你建議我做什麼?如何使變換算法在我的情況下可用? –

+0

@КристиянКостадинов使用'pair '而不是'pair ''? –

+0

已經嘗試過了,我仍然得到相同的錯誤 –

相關問題