当前位置: 首页 > 知识库问答 >
问题:

错误:C2679二进制 '==': 找不到运算符,该运算符采用“constd::string”类型的右操作数(或者没有可接受的转换

宗政元青
2023-03-14

我已经为一个员工管理系统编写了代码,该系统将员工类对象存储到一个向量中,在我尝试编译之前没有错误,我得到了错误:C2679二进制 '==': 找不到运算符,该运算符需要一个类型为“constd::string”的右操作数(或者没有可接受的转换)。但是我不确定为什么会有任何帮助,谢谢!

// 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;
}

共有2个答案

陆洛城
2023-03-14

在我看来(编辑:已注释掉)<代码>字符串响应 之前声明

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

希望这能给你指明正确的方向

编辑:它在那里,但被注释掉了。尝试:

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.

我会仔细检查所有代码,以避免注释干扰代码

曾阳飙
2023-03-14

std::find将比较emps中的员工*GetName返回的std::字符串。没有为员工*定义的比较运算符来执行此操作。我们可以做一个,但是因为GetName的行为是获取用户的名称,而不是简单地返回员工的名称,这将很快变得一团糟。

首先,停止在向量中存储指向员工的指针。这一简单的更改将消除您过去、现在和未来的绝大多数痛苦。一般来说,尽可能少地使用新代码(为什么C程序员应该尽量少使用“new”当您发现自己确实需要新的时,请选择智能指针。

vector<Employee*> emps;

成为

vector<Employee> emps;

这会对您的代码产生连锁反应,尤其是

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

必须成为

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

但是,也要查看“emplace\u back”,并强烈考虑获得用户输入,然后围绕它构建emp。

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

或朋友功能

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

然后将调用更改为查找以比较员工s

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

这可能会导致更多的问题,因为iter是一个常量迭代器和一个成员变量(Rubber Ducky想和您谈谈这一点)。也完全忽略了代码中还有几十个逻辑错误这一事实。

 类似资料: