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();
}
Whenever I run the program it gives me the following error ->
Error 7 error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const CDrug' (or there is no acceptable conversion)
This is because the maps elements are really that:
pair< const CDrug, ... >
not pair< CDrug, ... >
They key type is const because changing the key in an existing element of the map would cause trouble. (it would make the element unsorted thereby breaking some map invariant).
Therefore the object returned by your transform function can't be assigned to the map element => compilation failed.
Moreover you cannot use transform on a map because you can't assign to a map iterator ( because the key is const). So you should use a for_each instead, as described in a related question here: how to apply transform to a stl map in c++
Something like:
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);