C++ 重载赋值运算符不工作
我有一个班的员工。(当我添加成员任务和任务列表时,我的一些评论没有更新;对此我深表歉意。) 雇员C++ 重载赋值运算符不工作,c++,class,constructor,operator-overloading,copy-assignment,C++,Class,Constructor,Operator Overloading,Copy Assignment,我有一个班的员工。(当我添加成员任务和任务列表时,我的一些评论没有更新;对此我深表歉意。) 雇员 #include <string> #include <iostream> using namespace std; class Employee { private: string employee_name; string employee_ssn; string * taskList; //stores an array of tasks fo
#include <string>
#include <iostream>
using namespace std;
class Employee {
private:
string employee_name;
string employee_ssn;
string * taskList; //stores an array of tasks for the employee to do
int tasks; //stores the number of tasks an employee needs to do
public:
//constructors
Employee(); //default - nothing
Employee(string, string, string a[], int numOfTasks); //sets both ssn and name
~Employee(); //destructor
//copy constructor:
Employee(const Employee &emp);
Employee & operator =(const Employee& source);
void set_name(string); //sets name in program
void set_ssn(string); //sets ssn in program
string get_ssn(); //returns ssn as string
string get_name(); //returns emp name as string
void display(); //displays both on two separate lines
};
#包括
#包括
使用名称空间std;
班级员工{
私人:
员工姓名字符串;
字符串employee\u ssn;
string*taskList;//存储员工要执行的任务数组
int tasks;//存储员工需要执行的任务数
公众:
//建设者
Employee();//默认值-无
Employee(string,string,string a[],int numotasks);//设置ssn和名称
~Employee();//析构函数
//复制构造函数:
员工(施工人员和环境管理人员);
员工和操作员=(常量员工和来源);
void set_name(string);//在程序中设置名称
void set_ssn(string);//在程序中设置ssn
字符串get_ssn();//将ssn作为字符串返回
string get_name();//以字符串形式返回emp名称
void display();//显示在两个单独的行上
};
Employee.cpp
#include "Employee.h"
//constructors
//default constructor makes the object empty
Employee::Employee() {
taskList = nullptr;
return;
}
//constructor sets both name and ssn
Employee::Employee(string x, string y, string a[], int numOfTasks) {
employee_name = x;
employee_ssn = y;
tasks = numOfTasks;
taskList = a;
return;
}
//destructor
Employee::~Employee() {
delete [] taskList;
}
//copy constructor
Employee::Employee(const Employee & source) {
//copy simple member variables
employee_name = source.employee_name;
employee_ssn = source.employee_ssn;
tasks = source.tasks;
//allocate new dynamic array for taskList
taskList = new string[source.tasks];
//copy values from one taskList to another
for (int i = 0; i < tasks; i++)
taskList[i] = source.taskList[i];
return;
}
//assignment operator overloading
Employee & Employee::operator =(const Employee& source) {
cout << "Calling the assignment operator overloader.\n";
//check for self assignment
if (this == &source)
return *this; //avoid doing extra work
employee_name = source.employee_name;
employee_ssn = source.employee_ssn;
tasks = source.tasks;
cout << "Substituting 'task list'\n";
//delete former taskList
//if (taskList != nullptr)
delete[] taskList;
cout << "TaskList deleted.\n";
//allocate new one with same capacity
taskList = new string[source.tasks];
//copy values from one to the oher
for (int i = 0; i < tasks; i++)
taskList[i] = source.taskList[i];
cout << "Function complete.\n";
return *this;
}
//postcon: name is set to inputted string
void Employee::set_name(string s) {
employee_name = s;
return;
}
//postcon: ssn is set to inputted string
void Employee::set_ssn(string s) {
employee_ssn = s;
return;
}
//returns ssn as string
string Employee::get_ssn() {
return employee_ssn;
}
//returns employee name as string
string Employee::get_name() {
return employee_name;
}
//precon: name and ssn are both assigned
//postcon: name and ssn printed to the screen w/ labels on two lines
void Employee::display() {
cout << "Name: " << employee_name << endl;
cout << "SSN: " << employee_ssn << endl;
cout << "Tasks:\n";
for (int i = 0; i < tasks; i++)
cout << i + 1 << ". " << taskList[i] << endl;
return;
}
#包括“Employee.h”
//建设者
//默认构造函数使对象为空
雇员::雇员(){
taskList=nullptr;
返回;
}
//构造函数设置名称和ssn
Employee::Employee(字符串x、字符串y、字符串a[],int-numotasks){
员工姓名=x;
员工_ssn=y;
tasks=numOfTasks;
任务列表=a;
返回;
}
//析构函数
雇员::~Employee(){
删除[]任务列表;
}
//复制构造函数
雇员::雇员(常数雇员和来源){
//复制简单成员变量
employee_name=source.employee_name;
employee\u ssn=source.employee\u ssn;
tasks=source.tasks;
//为任务列表分配新的动态数组
taskList=新字符串[source.tasks];
//将值从一个任务列表复制到另一个任务列表
for(int i=0;i
Employee::Employee(string x, string y, string a[], int numOfTasks) {
employee_name = x;
employee_ssn = y;
tasks = numOfTasks;
taskList = a;
return;
}
您只需将传递的指针a
存储在数据成员taskList
中
主要是阵列
string tasks[2] = {"Send emails", "Prepare meeting brief"};
//Taks to be assigned to Michael:
string tasks2[3] = {"Stock up on pens", "Send emails", "Organize union"};
未动态分配。因此,您不能在复制分配运算符中为此类数组调用运算符delete[]
delete[] taskList;
您需要在构造函数中动态地为数组分配一个作为参数传递给构造函数的指针
另外请注意,在默认构造函数中,您需要将数据成员tasks
设置为0。我想知道您是否可以尝试制作一个较小(最小)的示例?我刚刚将此标记为已回答,但我将在将来记住此点;谢谢!
delete[] taskList;