gpt4 book ai didi

c++ - Complex 类中的重载运算符

转载 作者:搜寻专家 更新时间:2023-10-31 01:06:11 24 4
gpt4 key购买 nike

我使用重载运算符实现复杂类。编译后它给了我一些错误,我不知道如何解决它们。错误通常是关于“complex.cpp”无法识别 Complex 类型,即使“complex.h”没有错误。

复杂.h

    /*Definition of Complex Class. This class contains overloading operators.*/

#ifndef COMPLEX_H
#define COMPLEX_H

using std::ostream;
using std::istream;

class Complex{

friend ostream &operator<<(ostream&, const Complex &);
friend istream &operator>>(istream&, Complex &);

public:
Complex(double = 0.0, double = 0.0);//constructor
Complex operator+(const Complex &) const;//addition
Complex operator-(const Complex &) const;//subtraction
Complex operator*(const Complex &) const;//multiplication
const Complex &operator=(const Complex &);//assignment
bool const &operator==(const Complex &) const;//equivalent
bool const &operator!=(const Complex &) const;//not equivalent
private:
double real;//real part
double imaginary;//imaginary part
};

#endif

复杂.cpp:

 //Definition of Member Functions of the Complex Class

#include <iostream>

using std::cout;
using std::ostream;
using std::istream;

#include "complex.h"

//Constructor
Complex::Complex(double r, double i)
:real(r), imaginary(i){};

//Overloaded addition operator
Complex Complex::operator+(const Complex &operand2) const
{
return Complex(real + operand2.real, imaginary +operand2.imaginary);

};
//Overloaded subtraction operator
Complex Complex::operator-(const Complex &operand2) const
{
return Complex(real - operand2.real, imaginary - operand2.imaginary);

};
//Overloaded assignment operator
const Complex& Complex::operator=(const Complex &right)
{
real = right.real;
imaginary = right.imaginary;
return *this;
};
//Overloaded multiplication operator
Complex Complex::operator*(const Complex &operand2) const{

return((real *operand2.real)-(real*operand2.imaginary), (real*operand2.imaginary)-(imaginary*operand2.real));
}

bool Complex& Complex::operator==(const Complex &right) const{

if ((real == right.real) && (imaginary == right.imaginary))
return true;
else
return false;

}
bool Complex& Complex::operator!=(const Complex &right) const{

if ((real != right.real) && (imaginary != right.imaginary))
return true;
else
return false;

}
ostream &operator<<(ostream&output, const Complex &complex){//Print as Complex object as (a,b) with overloaded version

output << '(' << complex.real << "," << complex.imaginary << ')';//it allows usage as cout<<a<<b<<c
return output;

};
istream &operator>>(istream&input, Complex &complex){//Get input from the user.

input.ignore();//ignore '('
input >> complex.real;
input.ignore();//ignore ","
input >> complex.imaginary;
input.ignore();//ignore ')'
return input;//it allows usage as cin>>a>>b>>c
}

错误列表:

1- 语法错误:标识符 'Complex' 62 1

2- error C2065:'complex': 未声明的标识符 58 1

3- error C2065: 'complex' : undeclared identifier 65 1

4- error C2065: 'complex' : 未声明的标识符 67 1

5- error C2086: 'bool Complex' : 重新定义 48 1

6- 错误 C2143:语法错误:在“&”56 1 之前缺少“,”

7- 错误 C2143:语法错误:缺少“;”在“&”之前 40 18- 错误 C2143:语法错误:缺少“;”在 '&' 48 1 之前

9- error C2228: '.imaginary' 的左边必须有 class/struct/union 58 1

10- error C2228: '.imaginary' 的左边必须有 class/struct/union 67 1

11- error C2228: '.real' 的左边必须有 class/struct/union 58 1

12- error C2228: '.real' 的左边必须有 class/struct/union 65 1

13- error C2373: 'Complex::operator !=' :redefinition;不同的类型修饰符 48 1

14- error C2373: 'Complex::operator ==' : redefinition;不同的类型修饰符 40 1

15- 错误 C2556:“int &Complex::operator !=(const Complex &) const”:重载函数与“const bool &Complex::operator !=(const Complex &) const”的返回类型仅不同 48 1

16- 错误 C2556:“int &Complex::operator ==(const Complex &) const”:重载函数与“const bool &Complex::operator ==(const Complex &) const”的返回类型仅不同 40 1

17- 错误 C2805:二进制“运算符 >>”的参数太少 62 1

18- 错误 C4430:假定缺少类型说明符 -int。注意:C++ 不支持 default-int 40 1

19-错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int 48 1

20-错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int 56 1

最佳答案

您可以在下面看到所有代码:

#include <iostream>
using std::cout;
using std::ostream;
using std::istream;
using namespace std;

class Complex
{
private:
friend ostream &operator<<(ostream&, const Complex&);
friend istream &operator>>(istream&, Complex&);
double real;
double imaginary;
public:
Complex(double = 0.0, double = 0.0);
Complex operator+(const Complex&) const;
Complex operator-(const Complex&) const;
Complex operator*(const Complex&) const;
const Complex &operator=(const Complex &);
const bool operator==(const Complex&) const;
const bool operator!=(const Complex&) const;
};

Complex::Complex(double r, double i)
{
this->real = r;
this->imaginary = i;
}

Complex Complex::operator+(const Complex& operando2) const
{
return Complex(real + operando2.real,imaginary + operando2.imaginary);
}

Complex Complex::operator-(const Complex& operando2) const
{
return Complex(real - operando2.real,imaginary - operando2.imaginary);
}

const Complex& Complex::operator=(const Complex &right)
{
real = right.real;
imaginary = right.imaginary;
return *this;
}

Complex Complex::operator*(const Complex &operando2) const
{
return ((real * operando2.real) - (real * operando2.imaginary), (real * operando2.imaginary) - (imaginary*operando2.real));
}

const bool Complex::operator==(const Complex &right) const
{
if((real == right.real) && (imaginary == right.imaginary))
return true;
else
return false;
}

const bool Complex::operator!=(const Complex &right) const
{
if((real != right.real) || (imaginary != right.imaginary))
return true;
else
return false;
}

ostream &operator<<(ostream&output, const Complex &complex)
{
output << '(' << complex.real << "," << complex.imaginary << ')';
return output;
}

istream &operator>>(istream&input, Complex &complex)
{
input.ignore();
input >> complex.real;
input.ignore();
input >> complex.imaginary;
input.ignore();
return input;
}

main(void)
{
Complex c1(4,5);
Complex c2(8,9);
Complex c3 = c1 + c2;
Complex c4 = c2 - c3;
Complex c5 = c1 * c3;
Complex c6(4,5);
bool varBool = c6 == c1;
varBool = c6 != c2;

c1 = c2 = c3;

cout << c3 << endl;

cin >> c3;
cout << c3 << endl;
}

关于c++ - Complex 类中的重载运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21281460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com