我已经编写了一个员工管理系统的代码,它将Employee类对象存储到一个向量中,在我尝试编译之前我没有收到任何错误,我收到错误:C2679 binary'==':找不到运算符采用'const std :: 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;
}
答案 0 :(得分:1)
"all me at 41"
会将std::find
中的Employee *
与emps
返回的std::string
进行比较。没有为GetName
定义的比较运算符。我们可以创建一个,但因为Employee *
的行为是为用户获取和命名,而不是简单地返回GetName
的名称,这很快就会变得一团糟。
首先停止在Employee
s中存储指向Employee
的指针。这种简单的改变将消除你过去,现在和未来的绝大部分痛苦。一般情况下,尽量少使用vector
(Why should C++ programmers minimize use of 'new'?),当您确实需要new
,prefer a smart pointer。
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_back
。
emp
或bool operator==(const Employee & rhs) const
{
return m_Name == rhs.m_Name;
}
函数
friend
然后将通话更改为bool operator==(const Employee & lhs,
const Employee & rhs)
{
return lhs.m_Name == rhs.m_Name;
}
以比较find
s
Employee
这可能会导致更多问题,因为iter = find(emps.begin(), emps.end(), emp); // Trying to find the employee in the datbase.
是iter
和成员变量(Rubber Ducky wants a word with you about this)。同样完全忽略了代码中存在几十个逻辑错误的事实。
答案 1 :(得分:0)
在我看来(编辑:在{/ 1>}声明之前已经注释掉了string response;
声明
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.
我会仔细检查所有代码,以避免评论干扰代码