如何 implement/overload 我的 class 二元运算符
how to implement/overload binary operators for my class
我有问题。我创建了一个处理复数的程序,但我遇到重载运算符的问题。我的老师说我必须创造很多,他说我应该检查这个link Why should I overload a C++ operator as a global function (STL does) and what are the caveats?
1) 我必须创建 5 个运算符,它们是 class 的成员函数并且有一个参数:+、−、!、++、−−。那么
2) 我必须创建 5 个运算符,它们是 class 的成员函数,并且有两个参数:=、+=、-=、*=、/=;那么
3) 我必须创建 8 个运算符,它们是全局友元函数 +、-、*、/、==、!=、<<、>> 并带有两个参数。
我老师说我必须改变这些操作符的声明 2) 想想他们会做什么?你知道怎么改吗?
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;
}
运算符重载与其他函数的重载没有区别,所以不能更改函数签名,只能更改其实现。如果这是你大学学科的任务,那么继续实施所有运算符,否则你不需要为你构建的任何 class 提供所有运算符的自定义版本,但只有在你真正需要它们时才需要。
您获得了
的签名权
Comp operator+(const Comp& x);
Comp operator-(const Comp& x);
但不适用于
Comp operator-=(const Comp& x, int value)
...和其他人。
当作为成员函数实现时,运算符总是在 this
上运行。也就是说,您的 operator-=
将被称为
Comp a,b;
a.operator-=(b,42);
但你想要
a.operator-=(42);
我有问题。我创建了一个处理复数的程序,但我遇到重载运算符的问题。我的老师说我必须创造很多,他说我应该检查这个link Why should I overload a C++ operator as a global function (STL does) and what are the caveats? 1) 我必须创建 5 个运算符,它们是 class 的成员函数并且有一个参数:+、−、!、++、−−。那么
2) 我必须创建 5 个运算符,它们是 class 的成员函数,并且有两个参数:=、+=、-=、*=、/=;那么
3) 我必须创建 8 个运算符,它们是全局友元函数 +、-、*、/、==、!=、<<、>> 并带有两个参数。
我老师说我必须改变这些操作符的声明 2) 想想他们会做什么?你知道怎么改吗?
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;
}
运算符重载与其他函数的重载没有区别,所以不能更改函数签名,只能更改其实现。如果这是你大学学科的任务,那么继续实施所有运算符,否则你不需要为你构建的任何 class 提供所有运算符的自定义版本,但只有在你真正需要它们时才需要。
您获得了
的签名权Comp operator+(const Comp& x);
Comp operator-(const Comp& x);
但不适用于
Comp operator-=(const Comp& x, int value)
...和其他人。
当作为成员函数实现时,运算符总是在 this
上运行。也就是说,您的 operator-=
将被称为
Comp a,b;
a.operator-=(b,42);
但你想要
a.operator-=(42);