我应该如何在 C++ 中重载运算符 +
How am I supposed to overload the operator + in C++
我应该重载运算符 + 以添加两个小队,结果 return 一个新小队的成员人数是两个小队成员人数和其他属性的总和从成员较多的小队中抽取。我正在尝试,但结果没有任何意义!而且我不知道错误是在我重载 + 运算符的部分,还是在显示哪个旅游拥有最多成员的函数中?!
这是所有练习的内容:“山地小队问题 2 (0 / 30)
为登山小队写一个class,保存小队名称(动态分配的字符数组)、旅行次数(整数)和成员数量(整数)的信息。为此 class 工具:
•operator + 用于添加两个小队,结果 return 一个新小队的成员人数是两个小队成员人数的总和,其他属性从小队中获取更多的成员。
•运营商>, < 成员数量比较
•operator << 用于在 SO 上打印信息。
编写一个函数,接受山区小队数组和数组大小,并打印出拥有最大成员数的小队。
“
#include <iostream>
#include <string.h>
using namespace std;
class MSquad{
private:
char *name;
int tours;
int members;
void copy(const MSquad &toCopy){
name = new char[strlen(toCopy.name) + 1];
strcpy(name, toCopy.name);
tours = toCopy.tours;
members = toCopy.members;
}
public:
MSquad(char *n = "unknown", int nT = 0, int nM = 0){
name = new char[strlen(n + 1)];
strcpy(name, n);
tours = nT;
members = nM;
}
MSquad(const MSquad &toCopy){
copy(toCopy);
}
~MSquad(){
delete [] name;
}
const MSquad &operator=(const MSquad &right){
if(&right != this){ // avoiding self- assignment
delete [] name;
copy(right);
}
return *this;
}
MSquad &operator+(const MSquad &right) const{
members = members + right.members;
if(right.members > members){
name = new char[strlen(right.name) + 1];
strcpy(name, right.name);
tours = right.tours;
//members = members + right.members;
}
return *this;
}
bool operator>(const MSquad &right){
return members > right.members;
}
bool operator<(const MSquad &right){
return members < right.members;
}
friend ostream &operator<<(ostream &output, const MSquad &right);
friend void mostMembers(MSquad *squads, int size);
};
ostream &operator<<(ostream &output, const MSquad &right){
output << "Name: " << right.name;
output << " Tours: " << right.tours;
output << " Members: " << right.members << endl;
return output;
}
void mostMembers(MSquad squads[], int size){
int max = squads[0].members;
int j = 0;
for(int i = 1; i < size; i++){
if(squads[i].members >= max){
max = squads[i].members;
j = i;
}
}
cout << "The max number of members is in squad in: " << squads[j] << endl;
}
int main()
{
MSquad squads[3];
MSquad s;
for (int i=0;i<3;i++)
{
char name[100];
int tours;
int members;
cin>>name;
cin>>tours;
cin>>members;
squads[i] = MSquad(name, tours, members);
}
s = squads[0] + squads[1];
cout<<s;
mostMembers(squads, 3);
return 0;
}
你试过调试器吗?
或者,更好的是 - 在纸上解决您的问题 - 然后将其转化为代码。
问题是您没有理解 operator +
应该 return 的意思。根据您的分配:
•operator + for adding two squads that will return as a result a new
squad with number of members that is sum of the number of members of
both squads,
在您的代码中,您正在 return为 operator +
创建 MSquad&
。这是不正确的,因为您应该 returning 一个由当前对象和传入对象组成的新 MSquad
对象。
但是,一切都没有丢失,我们可以保留您现在拥有的代码。拯救我们的是你写了一个用户定义的复制构造函数和析构函数,所以我们可以应用一个简单的修复。
我们可以解决此问题的方法是执行以下操作(这不符合您的要求,但让我们使用您现在拥有的代码):
MSquad &operator+=(const MSquad &right) const
{
members = members + right.members;
if(right.members > members){
name = new char[strlen(right.name) + 1];
strcpy(name, right.name);
tours = right.tours;
//members = members + right.members;
}
return *this;
}
MSquad operator+(const MSquad &right) const
{
MSquad temp(*this); // create a temporary copy of the current object
return temp += right; // use += above on passed-in object and return object.
}
我们采用了您现有的代码,将其制成 operator+=
,并使用此函数创建了一个 operator +
。于是我们一石二鸟。我们创建了一个 operator +=
,它应该 return 对当前对象的引用,并使用 operator +=
作为辅助函数编写了 operator +
。另外请注意,我们利用复制构造函数创建了当前对象的临时对象。
一般来说,当您重载 +
等运算符时,最好先重载 op=
,然后从运算符 op
调用它。
请注意,您的代码还有许多其他问题,涉及指针管理不善和内存泄漏。例如,operator +
函数(答案中现在是 operator +=
)存在内存泄漏,因为您没有 delete []
之前的 name
分配。但是,我将由您来解决这些问题。
我应该重载运算符 + 以添加两个小队,结果 return 一个新小队的成员人数是两个小队成员人数和其他属性的总和从成员较多的小队中抽取。我正在尝试,但结果没有任何意义!而且我不知道错误是在我重载 + 运算符的部分,还是在显示哪个旅游拥有最多成员的函数中?!
这是所有练习的内容:“山地小队问题 2 (0 / 30)
为登山小队写一个class,保存小队名称(动态分配的字符数组)、旅行次数(整数)和成员数量(整数)的信息。为此 class 工具:
•operator + 用于添加两个小队,结果 return 一个新小队的成员人数是两个小队成员人数的总和,其他属性从小队中获取更多的成员。
•运营商>, < 成员数量比较
•operator << 用于在 SO 上打印信息。
编写一个函数,接受山区小队数组和数组大小,并打印出拥有最大成员数的小队。 “
#include <iostream>
#include <string.h>
using namespace std;
class MSquad{
private:
char *name;
int tours;
int members;
void copy(const MSquad &toCopy){
name = new char[strlen(toCopy.name) + 1];
strcpy(name, toCopy.name);
tours = toCopy.tours;
members = toCopy.members;
}
public:
MSquad(char *n = "unknown", int nT = 0, int nM = 0){
name = new char[strlen(n + 1)];
strcpy(name, n);
tours = nT;
members = nM;
}
MSquad(const MSquad &toCopy){
copy(toCopy);
}
~MSquad(){
delete [] name;
}
const MSquad &operator=(const MSquad &right){
if(&right != this){ // avoiding self- assignment
delete [] name;
copy(right);
}
return *this;
}
MSquad &operator+(const MSquad &right) const{
members = members + right.members;
if(right.members > members){
name = new char[strlen(right.name) + 1];
strcpy(name, right.name);
tours = right.tours;
//members = members + right.members;
}
return *this;
}
bool operator>(const MSquad &right){
return members > right.members;
}
bool operator<(const MSquad &right){
return members < right.members;
}
friend ostream &operator<<(ostream &output, const MSquad &right);
friend void mostMembers(MSquad *squads, int size);
};
ostream &operator<<(ostream &output, const MSquad &right){
output << "Name: " << right.name;
output << " Tours: " << right.tours;
output << " Members: " << right.members << endl;
return output;
}
void mostMembers(MSquad squads[], int size){
int max = squads[0].members;
int j = 0;
for(int i = 1; i < size; i++){
if(squads[i].members >= max){
max = squads[i].members;
j = i;
}
}
cout << "The max number of members is in squad in: " << squads[j] << endl;
}
int main()
{
MSquad squads[3];
MSquad s;
for (int i=0;i<3;i++)
{
char name[100];
int tours;
int members;
cin>>name;
cin>>tours;
cin>>members;
squads[i] = MSquad(name, tours, members);
}
s = squads[0] + squads[1];
cout<<s;
mostMembers(squads, 3);
return 0;
}
你试过调试器吗?
或者,更好的是 - 在纸上解决您的问题 - 然后将其转化为代码。
问题是您没有理解 operator +
应该 return 的意思。根据您的分配:
•operator + for adding two squads that will return as a result a new squad with number of members that is sum of the number of members of both squads,
在您的代码中,您正在 return为 operator +
创建 MSquad&
。这是不正确的,因为您应该 returning 一个由当前对象和传入对象组成的新 MSquad
对象。
但是,一切都没有丢失,我们可以保留您现在拥有的代码。拯救我们的是你写了一个用户定义的复制构造函数和析构函数,所以我们可以应用一个简单的修复。
我们可以解决此问题的方法是执行以下操作(这不符合您的要求,但让我们使用您现在拥有的代码):
MSquad &operator+=(const MSquad &right) const
{
members = members + right.members;
if(right.members > members){
name = new char[strlen(right.name) + 1];
strcpy(name, right.name);
tours = right.tours;
//members = members + right.members;
}
return *this;
}
MSquad operator+(const MSquad &right) const
{
MSquad temp(*this); // create a temporary copy of the current object
return temp += right; // use += above on passed-in object and return object.
}
我们采用了您现有的代码,将其制成 operator+=
,并使用此函数创建了一个 operator +
。于是我们一石二鸟。我们创建了一个 operator +=
,它应该 return 对当前对象的引用,并使用 operator +=
作为辅助函数编写了 operator +
。另外请注意,我们利用复制构造函数创建了当前对象的临时对象。
一般来说,当您重载 +
等运算符时,最好先重载 op=
,然后从运算符 op
调用它。
请注意,您的代码还有许多其他问题,涉及指针管理不善和内存泄漏。例如,operator +
函数(答案中现在是 operator +=
)存在内存泄漏,因为您没有 delete []
之前的 name
分配。但是,我将由您来解决这些问题。