我已经为一个员工管理系统编写了代码,该系统将员工类对象存储到一个向量中,在我尝试编译之前没有错误,我得到了错误: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;
}
在我看来(编辑:已注释掉)<代码>字符串响应 之前声明
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.
我会仔细检查所有代码,以避免注释干扰代码
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想和您谈谈这一点)。也完全忽略了代码中还有几十个逻辑错误这一事实。
我正在尝试编译以下代码: VS2012 C编译器返回以下编译错误: ... VC\包含\实用程序(219):错误C2678:二进制 '==': 找不到运算符,该运算符采用“const Point”类型的左侧操作数(或者没有可接受的转换) GCC C编译器返回以下编译错误: /usr/include/c /4.8/bits/stl_pair.h: 在'bool std::运算符==(constd::
二进制运算符"*"的操作数类型错误 我在编译过程中得到的错误是二进制运算符的坏操作数类型,表示:第一种类型:int第二种类型:int[],我只能使用这个逻辑。以下是我节目的一部分
我不知道如何修正我错误。错误状态 “DayCare.java:29:错误:二进制运算符”-“[numDaysString-1])的操作数类型不正确)第一类型:String第二类型:int”
二进制运算符' (字符串?)是什么类型?
错误显示这一行 我已经修复了代码: 是括号的一个问题,以后可以更新。
二进制运算符“*”的操作数类型不正确 我在编译过程中得到的错误是二进制运算符的操作数类型错误,它说:第一个类型:int,第二个类型:int[],我只能使用这个逻辑。以下是我的程序的一部分