变换算法给出 "binary '=' no operator which takes left-hand operand.."
transform algorithm gives "binary '=' no operator which takes left-hand operand.."
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();
}
每当我 运行 程序时,它都会给我以下错误 ->
Error 7 error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const CDrug' (or there is no acceptable conversion)
这是因为地图元素实际上是:
pair< const CDrug, ... >
不是 pair< CDrug, ... >
它们的键类型是常量,因为更改地图现有元素中的键会导致麻烦。 (这会使元素未排序,从而破坏某些映射不变性)。
因此,您的转换函数返回的对象无法分配给地图元素 => 编译失败。
此外,您不能在地图上使用转换,因为您不能分配给地图迭代器(因为键是常量)。
因此,您应该改用 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);
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();
}
每当我 运行 程序时,它都会给我以下错误 ->
Error 7 error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const CDrug' (or there is no acceptable conversion)
这是因为地图元素实际上是:
pair< const CDrug, ... >
不是 pair< CDrug, ... >
它们的键类型是常量,因为更改地图现有元素中的键会导致麻烦。 (这会使元素未排序,从而破坏某些映射不变性)。
因此,您的转换函数返回的对象无法分配给地图元素 => 编译失败。
此外,您不能在地图上使用转换,因为您不能分配给地图迭代器(因为键是常量)。 因此,您应该改用 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);