Guru of the Week 2 no match for the operator==
- by Adam
From Guru of the Week 2. We have the function:
string FindAddr(const list<Employee> l, string name)
{
for( list<Employee>::const_iterator i = l.begin(); i != l.end(); i++)
{
if( *i == name ) // here will be compilation error
{
return (*i).addr;
}
}
return "";
}
I added dummy Employee class to that:
class Employee
{
string n;
public:
string addr;
Employee(string name) : n(name) {}
Employee() {}
string name() const
{
return n;
}
operator string()
{
return n;
}
};
And got compilation error:
error: no match for ‘operator==’ in ‘i.std::_List_iterator<_Tp>::operator* [with _Tp = Employee]() == name’
It works only if add operator== to Employee. But, Herb Sutter wrote that:
The Employee class isn't shown, but for this to work it must either have a conversion to string or a conversion ctor taking a string.
But Employee has a conversion function and conversion constructor as well. GCC version 4.4.3. Compiled normally, g++ file.cpp without any flags.
There should be implicit conversion and it should work, why it doesn't?