简体   繁体   中英

Error:C2679 binary '==': no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion

I have written my code for an employee management system that stores Employee class objects into a vector, I am getting no errors until I try and compile, I am getting the error: C2679 binary '==': no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion). But I am not sure why any help would be great, Thank you!

// Employee Management System
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class Employee
{
public:
    Employee();
    string GetName();
    string GetStatus();
    float GetSalary();
    int GetAge();
    int GetYearHired();

private:
    string m_Name;
    string m_Status;
    float m_Salary;
    int m_Age;
    int m_YearHired;
};

Employee::Employee()
{
    m_Salary = 0;
    m_Age = 0;
    m_YearHired = 0;
}

string Employee::GetName()
{
    string fName;
    string lName;
    cout << "Please enter the new employee's first name: ";
    cin >> fName;
    cout << "Please enter the new employee's last name: ";
    cin >> lName;
    m_Name = fName + lName;
    return m_Name;
}

string Employee::GetStatus()
{
    string status;
    cout
            << "Please enter the employee's status (full time, part time, or manager): ";
    cin >> status;
    return m_Status;
}

float Employee::GetSalary()
{
    float salary;
    cout << "Please enter the employee's salary: ";
    cin >> salary;
    return m_Salary;
}

int Employee::GetAge()
{
    int age;
    while (true)
    {
        cout << "Please enter the employee's age: ";
        cin >> age;
        if (age > 0)
            break;
        else
            cout << "Error: Please enter a positive value.";
    }
    return m_Age;
}

int Employee::GetYearHired()
{
    int yearHired;
    cout << "Please enter what year the employee was hired: ";
    cin >> yearHired;
    return m_YearHired;
}

class Staff
{
    vector<Employee*> emps;
    vector<Employee*>::const_iterator iter;

public:
    Staff();
    virtual ~Staff();
    void Add();
    void Remove();
    void Clear();
    void Display();
};

Staff::Staff()
{
    emps.reserve(20);
}

Staff::~Staff()
{
    Clear();
}

void Staff::Add()
{
    Employee* emp = new Employee;
    emp->GetName();
    emp->GetStatus();
    emp->GetSalary();
    emp->GetAge();
    emp->GetYearHired();
    emps.push_back(emp);
}

void Staff::Remove()
{
    Employee* emp;
    cout << "Which employee would you like to remove?";
    emp->GetName();

    iter = find(emps.begin(), emps.end(), emp->GetName()); // Trying to find the employee in the datbase.
    if (iter != emps.end()) // If the employee is found in the vector it is removed.
    {
        cout << "\n" << *iter << " was removed\n\n";
        emps.erase(iter); // removes employee from the vector.
    }
    else // If the employee is not found in the vector, it tells the user that the employee was not found.
    {
        cout << "Employee not found, please choose anoter employee.\n\n";
    }
}

void Staff::Clear()
{
    cout << "\nDo you really want to clear all employees? (yes/no)\n"; // Asking the user if they want to clear the database.
    string response;
// Storing the response of the user.
    cin >> response; // Getting the users response (yes/no).
    if (response == "yes") // If response is yes.
    {
        vector<Employee*>::iterator iter = emps.begin(); // Declares an iterator for the emps vector and sets it to the beginning of the vector.
        for (iter = emps.begin(); iter != emps.end(); ++iter) // Iterates through vector.
        {
            delete *iter; // Deletes the iterators in the vector, freeing all memory on the heap.* iter = 0;
            // Sets iterator to zero so it does not become a dangling pointer.
        }
        emps.clear(); // Clear vector of pointers.
    }
    else // If response is no.
    {
        cout << "\nAll employee's remain in the database.\n";
    }
}

void Staff::Display()
{
    Employee* emp;
    if (emps.size() == 0) // Checking to see if the database is empty.
        cout
                << "\nThere are no employee's in the database, add employee's to view them here.\n ";
    else // If the cart contains any items.
    {
        cout << "\nThe database contains: \n";
        for (iter = emps.begin(); iter != emps.end(); ++iter) // Displaying the inventory.
        {
            cout << "-------------------------------------------------";
            cout << "Employee's Name         : " << emp->GetName() << endl;
            cout << "Employee's Status       : " << emp->GetStatus() << endl;
            cout << "Employee's Salary       : " << emp->GetSalary() << endl;
            cout << "Employee's Age          : " << emp->GetAge() << endl;
            cout << "Year employee was hired : " << emp->GetYearHired() << endl;
            cout << "-------------------------------------------------";
        }
    }
}

int main()
{
    int option = 0;
    Staff stf;
// Welcoming the user to the Employee Management System program.
    cout
            << "Welcome to our Employee Management System! To get started see the menu options below :\n ";

// Main loop
    while (option != 5) // The loop will repeat until the user enters 5 as the option.
    {
        cout << "------------------------------------------------------------------------------------- - ";
        cout << "\nMenu Options: \n";
        cout << "\nTo select an option, please type in the number that corresponds to that option.\n ";
        cout << "1 - Add an Employee\n2 - Remove an Employee\n3 - Clear the database\n4 - Display Employee's in Database\n5 - Quit" << endl;
        cout << "\nWhat would you like to do? ";
        cout << "------------------------------------------------------------------------------------- - ";

// Start of the validity check.
        bool validInput = false;

        while (!validInput) // The loop will repeat until the users input is valid.
        {
            cin >> option; // User inputs first option choice.
            validInput = true; // Assign the input as valid.
            if (cin.fail()) // Tests to make sure the value assigned is valid for the variable type.
            {
                cout << "\nPlease choose a menu option by number\n";
                cin.clear(); // Clears stream error.
                cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Removes an invalid characters.
                validInput = false; // Sets the input back to false, repeats the loop.
            }
        }

        switch (option)
        {
            case 1:
            {
                stf.Add();
                break;
            }
            case 2:
            {
                stf.Remove();
                break;
            }
            case 3:
            {
                stf.Clear();
                break;
            }
            case 4:
            {
                stf.Display();
                break;
            }
            case 5: // If option = 5.
                cout << "\nThank you for using the Employee Management Program!\n";
                // Thanks the user for using the Employee Management program.
                break;
            default: // If the user does not put in a valid option, it tells them to try again.
                cout << "\nThat's not a valid option. Please try again.\n";
                break;
        }
    }
    system("pause");
    return 0;
}

Problem

std::find is going to compare the Employee * s in emps against the std::string that is returned by GetName . There is no comparison operator defined for Employee * that does this. We can make one, but because the behaviour of GetName is to Get and name for the user, not simply return the Employee 's name this will quickly become a mess.

Solution

First Stop storing pointers to Employee s in the vector s. This simple change will eliminate the vast majority of your past, present and future pain. In general, use new as little as possible ( Why should C++ programmers minimize use of 'new'? ) and when you do find yourself needing new , prefer a smart pointer .

vector<Employee*> emps;

becomes

vector<Employee> emps;

that has a ripple effect through your code, not the least of which is

void Staff::Add()
{
    Employee* emp = new Employee;
    emp->GetName();
    emp->GetStatus();
    emp->GetSalary();
    emp->GetAge();
    emp->GetYearHired();
    emps.push_back(emp);
}

must become

void Staff::Add()
{
    Employee emp;
    emp.GetName();
    emp.GetStatus();
    emp.GetSalary();
    emp.GetAge();
    emp.GetYearHired();
    emps.push_back(emp);
}

But also look into emplace_back and strongly consider getting the user input and then constructing emp around it.

bool operator==(const Employee & rhs) const
{
    return m_Name == rhs.m_Name;
}

or a friend function

bool operator==(const Employee & lhs,
                const Employee & rhs)
{
    return lhs.m_Name == rhs.m_Name;
}

and then change the call to find to compare Employee s

iter = find(emps.begin(), emps.end(), emp); // Trying to find the employee in the datbase.

This may lead to more problems because iter is a const_iterator and a member variable ( Rubber Ducky wants a word with you about this ). Also completely ignores the fact that there are a few dozen more logical mistakes in the the code.

It seems to me (EDIT: had a commented out) string response; declaration before

cin >> response; // Getting the users response (yes/no).

Hope this points you in the right direction

EDIT: It's there but it's commented out. Try:

cout << "\nDo you really want to clear all employees? (yes/no)\n"; 
// Asking the user if they want to clear the database.
string response;
// Storing the response of the user.
cin >> response; // Getting the users response (yes/no).
if (response == "yes") // If response is yes.

And I'd double check all the code to avoid the comments interfering with the code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::string_view' (or there is no acceptable conversion) Error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'const char [4]' (or there is no acceptable conversion) 22 error C2679: binary '[' : no operator found which takes a right-hand operand of type 'const VerseKey' (or there is no acceptable conversion) error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::vector<_Ty> *' (or there is no acceptable conversion) Error C++ 2679 (binary '>>': no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion)) error C2679: binary '<<':no operator found which takes a right-hand operand of type 'std::string' C2679 binary '-=': no operator found which takes a right-hand operand of type 'T' (or there is no acceptable conversion) c++ error c2679: binary '[' : no operator found which takes a right-hand operand of type 'SalesItem' (or there is no acceptable conversion)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM