C++ 如何为我的类实现/重载二进制运算符
我有个问题。我创建了一个可以处理复数的程序,但是我遇到了重载运算符的问题。我的老师说我必须创建很多,他说我应该检查这个链接 1) 我必须创建5个运算符,它们是类的成员函数,并且只有一个参数:+,−, !, ++, −−. 然后 2) 我必须创建5个运算符,它们是类的成员函数,有两个参数:=,+=,−=, *=, /=; 然后 3) 我必须创建8个操作符,它们是全局友元函数+,−, *, /, ==, !=, 取两个参数 我的老师说我必须改变这些操作员的声明,并考虑他们将做什么?你知道如何改变它们吗C++ 如何为我的类实现/重载二进制运算符,c++,class,oop,operator-overloading,C++,Class,Oop,Operator Overloading,我有个问题。我创建了一个可以处理复数的程序,但是我遇到了重载运算符的问题。我的老师说我必须创建很多,他说我应该检查这个链接 1) 我必须创建5个运算符,它们是类的成员函数,并且只有一个参数:+,−, !, ++, −−. 然后 2) 我必须创建5个运算符,它们是类的成员函数,有两个参数:=,+=,−=, *=, /=; 然后 3) 我必须创建8个操作符,它们是全局友元函数+,−, *, /, ==, !=, 取两个参数 我的老师说我必须改变这些操作员的声明,并考虑他们将做什么?你知道如何改变它
Comp operator=(const Comp x) = default;
Comp operator-=(const Comp& x, int value)
{
x.real -= value;
x.imag -= value;
return x;
}
Comp operator+=(const Comp& x, const Comp& y)
{
x.real += y.real;
x.imag += y.imag;
return x;
}
Comp operator*=(const Comp& x, const Comp& y)
{
x.real *= y.real;
x.imag *= y.imag;
return x;
}
Comp operator/=(const Comp& x, const Comp& y)
{
x.real /= y.real;
x.imag /= y.imag;
return x;
}
我犯了很多错误:
complex.cpp:74:45: error: ‘Comp Comp::operator-=(const Comp&, int)’ must take exactly one argument
Comp operator-=(const Comp& x, int value)
^
complex.cpp:80:49: error: ‘Comp Comp::operator+=(const Comp&, const Comp&)’ must take exactly one argument
Comp operator+=(const Comp& x, const Comp& y)
^
complex.cpp:86:49: error: ‘Comp Comp::operator*=(const Comp&, const Comp&)’ must take exactly one argument
Comp operator*=(const Comp& x, const Comp& y)
^
complex.cpp:92:49: error: ‘Comp Comp::operator/=(const Comp&, const Comp&)’ must take exactly one argument
Comp operator/=(const Comp& x, const Comp& y)
^
complex.cpp:73:10: error: defaulted declaration ‘Comp Comp::operator=(Comp)’
Comp operator=(const Comp x) = default;
^~~~~~~~
complex.cpp:73:10: error: does not match expected signature ‘constexpr Comp& Comp::operator=(Comp&)’
我还想知道这些操作员是否可以
Comp operator+(const Comp& x);
Comp operator-(const Comp& x);
bool operator!(void);
const Comp& operator++()
{
return *this;
}
const Comp operator++(int)
{
Comp temp(*this);
operator++();
return temp;
}
const Comp& operator--()
{
return *this;
}
const Comp operator--(int)
{
Comp temp(*this);
operator--();
return temp;
}
这是我的全部密码
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
using namespace std;
class Comp {
double real, imag;
public:
Comp(){
real=0;
imag=0;
}
double re(void) const
{
return real;
}
double im(void) const
{
return imag;
}
double mod(void) const
{
return sqrt(re()*re() + im()*im());
}
double arg(void) const
{
double faza;
if (im() >= 0)
faza = acos(re()/mod());
else
faza = 2*M_PI - acos(re()/mod());
return faza;
}
const Comp conj(void) const
{
Comp temp;
temp.real = re();
temp.imag = -im();
return temp;
}
~Comp(){}
Comp operator+(const Comp& x);
Comp operator-(const Comp& x);
bool operator!(void);
const Comp& operator++()
{
return *this;
}
const Comp operator++(int)
{
Comp temp(*this);
operator++();
return temp;
}
const Comp& operator--()
{
return *this;
}
const Comp operator--(int)
{
Comp temp(*this);
operator--();
return temp;
}
Comp operator=(const Comp x) = default;
Comp operator-=(const Comp& x, int value)
{
x.real -= value;
x.imag -= value;
return x;
}
Comp operator+=(const Comp& x, const Comp& y)
{
x.real += y.real;
x.imag += y.imag;
return x;
}
Comp operator*=(const Comp& x, const Comp& y)
{
x.real *= y.real;
x.imag *= y.imag;
return x;
}
Comp operator/=(const Comp& x, const Comp& y)
{
x.real /= y.real;
x.imag /= y.imag;
return x;
}
Comp operator=(const Comp x, const Comp y);
Comp operator-=(const Comp& x, const Comp& y);
Comp operator+=(const Comp& x, const Comp& y);
Comp operator*=(const Comp& x, const Comp& y);
Comp operator/=(const Comp& x, const Comp& y);
friend const Comp operator+(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real + y.real;
temp.imag = x.imag + y.imag;
return temp;
}
friend const Comp operator-(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real - y.real;
temp.imag = x.imag - y.imag;
return temp;
}
friend const Comp operator*(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real * y.real;
temp.imag = x.imag * y.imag;
return temp;
}
friend const Comp operator/(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real / y.real;
temp.imag = x.imag / y.imag;
return temp;
}
friend bool operator==(const Comp& x, const Comp& y)
{
if (x.real == y.real && x.imag == y.imag)
return 1;
else
return 0;
}
friend bool operator!=(const Comp& x, const Comp& y)
{
if (x.real != y.real || x.imag != y.imag)
return 1;
else
return 0;
}
friend std::ostream& operator<<(std::ostream& wart1, const Comp& a)
{
return wart1 <<fixed << setprecision(2) << '(' << a.re() << "," << a.im() << ')' << ' ' << endl;
}
friend std::istream& operator>>(std::istream& wart2, Comp& b){
char c;
return wart2>>c>>b.real>>c>>b.imag>>c;
}
};
int main(int argc, char* argv[])
{
ifstream read(argv[1]);
if (!read)
{ cerr << "Open error: " << argv[1] << endl; exit(1);}
ofstream write(argv[2]);
if(!write) { cerr << "Open error: " << argv[2] << endl; exit(2);}
read.clear();
read.seekg(0);
Comp x1;
read >> x1;
write << x1;
cout << x1;
Comp x2;
read >> x2;
write << x2;
cout << x2;
cout << x1.mod() << endl;
cout << x2.mod() << endl;
cout << x1.arg() << endl;
cout << x2.arg() << endl;
cout << x1.conj();
cout << x2.conj();
write << x2;
write << x1.mod() << endl;
write << x2.mod() << endl;
write << x1.arg() << endl;
write << x2.arg() << endl;
write << x1.conj();
write << x2.conj();
Comp sum;
sum = x1 + x2;
cout << sum;
write << sum;
Comp sub;
sub = x1 - x2;
cout << sub;
write << sub;
Comp mult;
mult = x1 * x2;
cout << mult;
write << mult;
Comp div;
div = x1 / x2;
cout << div;
write << div;
return 0;
}
#包括
#包括
#包括
#包括
#包括
#伊夫德夫·穆皮
#定义M_PI 3.14159265358979323846
#恩迪夫
使用名称空间std;
班级薪酬{
双实数,imag;
公众:
Comp(){
实数=0;
imag=0;
}
双重(空)常数
{
回归真实;
}
双im(无效)常数
{
返回图像;
}
双模(空)常数
{
返回sqrt(re()*re()+im()*im());
}
双参数(无效)常数
{
双faza;
如果(im()>=0)
faza=acos(re()/mod());
其他的
faza=2*M_PI-acos(re()/mod());
返回法扎;
}
常量Comp conj(void)常量
{
补偿温度;
temp.real=re();
temp.imag=-im();
返回温度;
}
~Comp(){}
Comp运算符+(常数Comp&x);
Comp操作符-(const Comp&x);
布尔运算符!(无效);
常量Comp&运算符++()
{
归还*这个;
}
常量Comp运算符++(int)
{
补偿温度(*本);
运算符++();
返回温度;
}
常量组件和运算符--()
{
归还*这个;
}
常量Comp运算符--(int)
{
补偿温度(*本);
运算符——();
返回温度;
}
复合运算符=(常数复合x)=默认值;
补偿运算符-=(常数补偿&x,整数值)
{
x、 实际-=价值;
x、 imag-=值;
返回x;
}
补偿运算符+=(常数补偿与x、常数补偿与y)
{
x、 real+=y.real;
x、 imag+=y.imag;
返回x;
}
补偿运算符*=(常数补偿与x、常数补偿与y)
{
x、 real*=y.real;
x、 imag*=y.imag;
返回x;
}
补偿运算符/=(常数补偿与x、常数补偿与y)
{
x、 real/=y.real;
x、 imag/=y.imag;
返回x;
}
复合运算符=(常数复合x,常数复合y);
补偿运算符-=(常数补偿&x,常数补偿&y);
补偿运算符+=(常数补偿&x,常数补偿&y);
补偿运算符*=(常数补偿&x,常数补偿&y);
补偿运算符/=(常数补偿&x,常数补偿&y);
友元常量Comp运算符+(常量Comp&x、常量Comp&y)
{
补偿温度;
temp.real=x.real+y.real;
温度imag=x.imag+y.imag;
返回温度;
}
friend const Comp操作符-(const Comp&x,const Comp&y)
{
补偿温度;
temp.real=x.real-y.real;
温度imag=x.imag-y.imag;
返回温度;
}
friend const Comp operator*(const Comp&x、const Comp&y)
{
补偿温度;
temp.real=x.real*y.real;
温度imag=x.imag*y.imag;
返回温度;
}
友元常量Comp运算符/(常量Comp&x,常量Comp&y)
{
补偿温度;
实际温度=x实际值/y实际值;
温度imag=x.imag/y.imag;
返回温度;
}
友元布尔运算符==(常数Comp&x,常数Comp&y)
{
if(x.real==y.real&&x.imag==y.imag)
返回1;
其他的
返回0;
}
友元布尔运算符!=(常数Comp&x,常数Comp&y)
{
if(x.real!=y.real | | x.imag!=y.imag)
返回1;
其他的
返回0;
}
friend std::奥斯特雷姆和运营商;
}
};
int main(int argc,char*argv[])
{
ifstream读取(argv[1]);
如果(!读取)
{cerr运算符重载与其他函数的重载没有什么不同,因此您不能更改函数签名,只能更改其实现。如果这是大学科目的任务,请继续执行并实现所有运算符,否则您不需要为您构建的任何类提供所有运算符的自定义版本,但是只有当你真的需要它们的时候。运算符重载与其他函数的重载没有什么不同,因此你不能更改函数签名,只能更改其实现。如果这是你大学科目的任务,那么继续执行并实现所有运算符,否则你不需要提供所有运算符的自定义版本RATOR适用于您构建的任何类,但仅在您真正需要它们时使用。您获得了正确的签名
Comp operator+(const Comp& x);
Comp operator-(const Comp& x);
但不是为了
Comp operator-=(const Comp& x, int value)
…和其他人
当实现为成员函数时,运算符总是在上操作此
。即您的运算符-=
将被称为
Comp a,b;
a.operator-=(b,42);
但是你想要
a.operator-=(42);
你的签名是对的
Comp operator+(const Comp& x);
Comp operator-(const Comp& x);
但不是为了
Comp operator-=(const Comp& x, int value)
…和其他人
当实现为成员函数时,运算符总是在上操作此
。即您的运算符-=
将被称为
Comp a,b;
a.operator-=(b,42);
但是你想要
a.operator-=(42);
错误输出实际上指向了确切的问题:complex.cpp:74:45:error:“Comp Comp::operator-=(const Comp&,int)”必须只接受一个参数Comp operator-=(const Comp&x,int value).operator-=
需要一个参数,而不是两个参数。可能需要阅读运算符重载:。下面有一个例子,运算符=*表示底部的分数,这应该很有用。也可以阅读:不相关,但如果Comp
表示复杂,则您的*运算符不遵循复杂算术的规则…一个好的、更高级别的规则是test y