bool operator!=(Complex& D) { return(*this != D); }这样写有问题吗
时间: 2024-12-11 18:20:27 浏览: 11
在这个上下文中,`operator!=` 是用于比较两个 `num_item_stu` 或者 `NumItem` 对象的不等于操作符。然而,您给出的代码片段似乎是在讨论 `Complex` 类型的操作符重载,但这里引用的内容与 `num_item_stu` 相关。因此,如果您的意图是针对 `Complex` 类型的,那么这段代码应该写成:
```cpp
bool Complex::operator!=(const Complex& D) {
return (*this) != D;
}
```
这表示对 `Complex` 类的一个实例(`*this`)与另一个 `Complex` 实例 `D` 进行不等于(`!=`)比较。如果两者相等则返回 `false`,反之返回 `true`。
需要注意的是,如果您的类名为 `num_item_stu` 或 `NumItem`,并且想要定义 `!=` 操作符,你应该按照类似的方式重载它:
```cpp
// 如果是 num_item_stu 或 NumItem 类
bool num_item_stu::operator!=(const num_item_stu& other) {
return (this->num != other.num) && (this->type != other.type);
}
```
相关问题
#include<iostream> using namespace std; class Complex { private: double real, image; public: Complex(double rel = 0, double img = 0) { real = rel; image = img; } void display() { cout << "(" << real; if (image > 0) cout << "+" << image << "*i)"; else if (image < 0) cout << image << "*i)"; else cout << ")"; } friend Complex operator -( Complex& c1, const Complex& c2); friend Complex operator /( Complex& c1, const Complex& c2); friend Complex operator ==(Complex& c1, Complex& c2); friend Complex operator !=(Complex& c1, Complex& c2); friend ostream operator <<(Complex& c1, Complex& c2); }; Complex operator -( Complex& c1, const Complex& c2) { Complex temp; temp.real = c1.real - c2.real; temp.image = c1.image - c2.image; return temp; } Complex operator /( Complex& c1, const Complex& c2) { Complex FFF; FFF.real = c1.real / c2.real; FFF.image = c1.image / c2.image; return FFF; } int main() { Complex c1(10, 9), c2(3, -4), c3; c3 = c1 / 5; c1.display(); cout << "/"; cout << 5; cout << "="; c3.display(); cout << endl; return 0; //输出比较的结果 cout<<"c1==c2 ? 结果:"; cout<<(c1==c2)<<endl; cout<<"c1!=c2 ? 结果:"; cout<<(c1!=c2)<<endl; }严重性 代码 说明 项目 文件 行 禁止显示状态 错误 C2678 二进制“<<”: 没有找到接受“std::ostream”类型的左操作数的运算符(或没有可接受的转换)帮我修改并给出正确代码
需要将友元函数的返回类型改为 ostream&,并在函数内部将输出语句改为 return os,同时在 main 函数内输出对象的语句需要改为 cout << c1 << endl; 和 cout << c2 << endl;。代码如下:
```
#include<iostream>
using namespace std;
class Complex {
private:
double real, image;
public:
Complex(double rel = 0, double img = 0) {
real = rel;
image = img;
}
void display() {
cout << "(" << real;
if (image > 0)
cout << "+" << image << "*i)";
else if (image < 0)
cout << image << "*i)";
else
cout << ")";
}
friend Complex operator -(Complex& c1, const Complex& c2);
friend Complex operator /(Complex& c1, const Complex& c2);
friend bool operator ==(Complex& c1, Complex& c2);
friend bool operator !=(Complex& c1, Complex& c2);
friend ostream& operator <<(ostream& os, Complex& c);
};
Complex operator -(Complex& c1, const Complex& c2) {
Complex temp;
temp.real = c1.real - c2.real;
temp.image = c1.image - c2.image;
return temp;
}
Complex operator /(Complex& c1, const Complex& c2) {
Complex FFF;
FFF.real = c1.real / c2.real;
FFF.image = c1.image / c2.image;
return FFF;
}
bool operator ==(Complex& c1, Complex& c2) {
return (c1.real == c2.real) && (c1.image == c2.image);
}
bool operator !=(Complex& c1, Complex& c2) {
return !(c1 == c2);
}
ostream& operator <<(ostream& os, Complex& c) {
os << "(" << c.real;
if (c.image > 0)
os << "+" << c.image << "*i)";
else if (c.image < 0)
os << c.image << "*i)";
else
os << ")";
return os;
}
int main() {
Complex c1(10, 9), c2(3, -4), c3;
c3 = c1 / 5;
c1.display();
cout << "/";
cout << 5;
cout << "=";
c3.display();
cout << endl;
cout << "c1 == c2 ? 结果:";
cout << (c1 == c2) << endl;
cout << "c1 != c2 ? 结果:";
cout << (c1 != c2) << endl;
cout << c1 << endl;
cout << c2 << endl;
return 0;
}
```
#include <iostream> using namespace std; class Complex { private: double real,image; //分别为实部、虚部 public: Complex(double rel=0, double img=0) { real=rel; image=img; } void display() //输出(a+b*i) 、(a-b*i) 、(a)等形式 { cout<<" ("<<real; if (image>0) cout<<"+"<<image<<"*i) "; //虚部为正 else if (image<0) cout<<image<<"*i) "; //虚部为负 else cout<<") "; //虚部为0,即为实数 } friend Complex operator -(Complex & c1,Complex & c2);//用友元函数重载 }; //友元函数的定义 Complex operator -(Complex & c1,Complex & c2) { Complex temp; temp.real=c1.real-c2.real; temp.image=c1.image-c2.image; return temp; } int main() { Complex c1(10,9), c2(3,-4),c3; c3=c1/5; //输出除的结果 c1.display(); cout<<"/"; cout<<5; cout<<"="; c3.display(); cout<<endl; //输出比较的结果 cout<<"c1==c2 ? 结果:"; cout<<(c1==c2)<<endl; cout<<"c1!=c2 ? 结果:"; cout<<(c1!=c2)<<endl; return 0; }
这段代码存在一个问题,即在主函数中调用了一个未定义的除法运算符“/”。同时,代码中定义了一个友元函数,用于重载减法运算符“-”,但是并没有使用该运算符,因此友元函数定义也无意义。
以下是修改后的代码:
```
#include <iostream>
using namespace std;
class Complex {
private:
double real, image; // 分别为实部、虚部
public:
Complex(double rel = 0, double img = 0)
{
real = rel;
image = img;
}
void display() // 输出(a+b*i)、(a-b*i)、(a)等形式
{
cout << " (" << real;
if (image > 0)
cout << "+" << image << "*i) "; // 虚部为正
else if (image < 0)
cout << image << "*i) "; // 虚部为负
else
cout << ") "; // 虚部为0,即为实数
}
// 重载减法运算符“-”
Complex operator-(Complex& c) {
Complex temp;
temp.real = real - c.real;
temp.image = image - c.image;
return temp;
}
// 重载相等运算符“==”
bool operator==(Complex& c) {
return (real == c.real) && (image == c.image);
}
// 重载不等运算符“!=”
bool operator!=(Complex& c) {
return !(*this == c);
}
};
int main() {
Complex c1(10, 9), c2(3, -4), c3;
c3 = c1 - c2; // 使用重载的减法运算符“-”
c1.display();
cout << "-";
c2.display();
cout << "=";
c3.display();
cout << endl;
cout << "c1==c2 ? 结果:";
cout << (c1 == c2) << endl;
cout << "c1!=c2 ? 结果:";
cout << (c1 != c2) << endl;
return 0;
}
```
修改后的代码中,重载了减法运算符“-”,并在主函数中使用该运算符计算了两个复数的差。同时,还重载了相等运算符“==”和不等运算符“!=”,并在主函数中使用这两个运算符比较了两个复数的大小关系。
阅读全文