重载赋值运算符不工作
overloaded assignment operator not working
我有一个class员工。 (自从我添加成员任务和任务列表后,我的一些评论没有更新;对此我深表歉意。)
Employee.h
#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
};
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 对象。
我似乎遇到的问题是使用赋值重载进行交换。
employee_driver.cpp
#include "Employee.h"
#include <iostream>
int main() {
//tasks for each employee to do:
//Tasks to be assigned to Marcy:
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"};
Employee *emp1 = new Employee("Marcy", "678091234", tasks, 2);
Employee *emp2 = new Employee("Michael", "123994567", tasks2, 3);
//display data before swap
emp1->display();
cout << endl;
emp2->display();
cout << endl;
//swap employees
Employee temp(*emp1); //using copy constructor to copy first employee into temporary
*emp1 = *emp2;
*emp2 = temp; //uses overloaded assignment operator to copy values of temp into emp2; Marcy's data is now in Michael's pointer
//display after swap
cout << "\n\nAfter swap:\n\n";
emp1->display();
cout << endl;
emp2->display();
//free heap
delete emp1;
delete emp2;
//delete emp3;
return 0;
}
问题似乎出现在这里:
*emp1 = *emp2;
(接近主程序的底部),但我不明白为什么;任何帮助,将不胜感激。我可以绕过它,但我认为这不是练习的目的,我想知道为什么这个语句不能正常工作。
谢谢。
在构造函数中
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。
我有一个class员工。 (自从我添加成员任务和任务列表后,我的一些评论没有更新;对此我深表歉意。)
Employee.h
#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
};
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 对象。
我似乎遇到的问题是使用赋值重载进行交换。
employee_driver.cpp
#include "Employee.h"
#include <iostream>
int main() {
//tasks for each employee to do:
//Tasks to be assigned to Marcy:
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"};
Employee *emp1 = new Employee("Marcy", "678091234", tasks, 2);
Employee *emp2 = new Employee("Michael", "123994567", tasks2, 3);
//display data before swap
emp1->display();
cout << endl;
emp2->display();
cout << endl;
//swap employees
Employee temp(*emp1); //using copy constructor to copy first employee into temporary
*emp1 = *emp2;
*emp2 = temp; //uses overloaded assignment operator to copy values of temp into emp2; Marcy's data is now in Michael's pointer
//display after swap
cout << "\n\nAfter swap:\n\n";
emp1->display();
cout << endl;
emp2->display();
//free heap
delete emp1;
delete emp2;
//delete emp3;
return 0;
}
问题似乎出现在这里:
*emp1 = *emp2;
(接近主程序的底部),但我不明白为什么;任何帮助,将不胜感激。我可以绕过它,但我认为这不是练习的目的,我想知道为什么这个语句不能正常工作。
谢谢。
在构造函数中
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。