0

I'm fairly new to C++ programming. I currently have an error C2678: binary '==': no operator found which takes a left-hand operand of type 'const Employee'

This error is point to line

if (*iLoc == aID)

I believe I am suppose to create a

bool Employee::operator==(const Employee & rhs) const
{
}

that will override the error. I think I need to make a overloaded == operator that is able to compare and unsigned int to an Employee object. But I am stuck. I was wondering if anyone is able to help. Part of my main.cpp is included.

bool Employee::operator==(const Employee & rhs) const
    {
}

void searchItem(const List<Employee>& l1)
{
if (l1.size() == 0)
    cout << "List empty!\n";

else
{

    unsigned int ID;
    cout << "Enter ID#: ";
    cin >> ID;

    size_t index = 0;
    List<Employee>::const_iterator iLoc = l1.begin();
    while (iLoc != l1.end())
    {
        if (*iLoc == aID)
        {
            cout << "Employee's Name:  " << aID << " found at node # " << index << endl;
            break;
        }
        index++;
        iLoc++;
    }
    if (iLoc == l1.end())
        cout << "ID# " << aID << " not found!\n";
}
}

void removeItem(List<Employee>& l1)
{
    if (l1.size() == 0)
        cout << "List empty!\n";
    else
    {
        unsigned int aID;
    cout << "Enter ID#: ";
    cin >> aID;

    size_t index = 0;
    List<Employee>::iterator iLoc = l1.begin();
    while (iLoc != l1.end())
    {
        if (*iLoc == aID)
        {
            cout << "Value " << aID << " removed from node # " << index << endl;
            l1.erase(iLoc);
            break;
        }
        index++;
        iLoc++;
    }
    if (iLoc == l1.end())
        cout << "Value not found!\n";
}
}
OrangePineapple
  • 65
  • 2
  • 2
  • 12

1 Answers1

0

In the function void removeItem(List<Employee>&), in the line

if (*iLoc == aID)

you are comparing a dereferenced iterator, i.e. an Employee, to an unsigned int (aID). The right hand side must be an Employee also, or Employee must have an implicit constructor that takes an unsigned int as a parameter, so the compiler may be able to convert aID to Employee(aID) when trying to resolve the call to (*iLoc).operator==(aID).

Solution: define either

bool Employee::operator==(const Employee & rhs) const
{
    return comparison_here;
}

and make sure Employee has a constructor that takes an unsigned int, or create an overloaded operator== that takes an unsigned int,

bool Employee::operator==(unsigned int rhs) const
{
    return comparison_here;
}

You may also consider defining operator== as a non-member function, so it can also take unsigned int as a left-hand-side parameter. More details: Operator overloading

Community
  • 1
  • 1
vsoftco
  • 55,410
  • 12
  • 139
  • 252