如何为我的类实现/重载二进制运算符
how to implement/overload binary operators for my class
>我有一个问题。我创建了一个程序,它适用于复数,但我在重载运算符时遇到了问题。我的老师说我必须创建很多,他说我应该检查这个链接 为什么要将C++运算符重载为全局函数(STL 确实如此(,有哪些注意事项? 1( 我必须创建 5 个运算符,它们是类的成员函数,并且有一个参数:+、−、!、++、−−。然后
2(我必须创建5个运算符,它们是类的成员函数和两个参数:=,+=,−=,*=,/=;然后
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;
}
你得到了正确的签名
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);
运算符重载与其他函数的重载没有什么不同,因此您不能更改函数签名,而只能更改其实现。如果这是你的大学科目的任务,那么继续实现所有运算符,否则你不需要为你构建的任何类提供所有运算符的自定义版本,而只是当你真正需要它们时。
相关文章:
- 重载==不适用于二进制树
- 正在尝试重载二进制搜索树分配运算符
- 如何为我的类实现/重载二进制运算符
- 你能帮我了解重载一元运算符和二进制运算符之间的区别吗?
- <未解析的重载函数类型>调用二进制谓词
- 铛.嵌套模板的二进制运算符重载
- 重载二进制操作的正确方法
- 运算符重载:简单添加...错误 C2677:二进制"+":未找到采用类型 ___ 的全局运算符(或者不存在可接受的转换)
- 如何在C++中重载一元和二进制减号运算符
- 重载的"运算符+"必须是一元或二进制运算符错误
- 在C++中,如何在派生类中重载二进制运算符
- 如果二进制运算符+重载的返回值是const,它会干扰优化吗
- 重载运算符 +,用于在二进制树中插入节点
- 赋值重载显式调用析构函数(在执行其他二进制重载运算符之后)
- 使用二进制搜索树重载运算符
- 在有根的二进制树模板类中重载了operator=()
- C std::lower_bound,使用重载运算符作为二进制谓词组合
- 错误:"int"和"<未解析的重载函数类型>"到二进制"运算符/"的操作数无效
- 重载二进制树的++运算符
- c++中为一个操作数重载二进制操作符