我使用重载运算符实现复杂类。编译后它给了我一些错误,我不知道如何解决它们。错误通常是“complex.cpp”无法识别复杂类型,即使“complex.h”没有错误。< / p>
complex.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
complex.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-错误C2065:'complex':未声明的标识符58 1
3-错误C2065:'complex':未声明的标识符65 1
4-错误C2065:'复杂':未声明的标识符67 1
5-错误C2086:'bool Complex':重新定义48 1
6-错误C2143:语法错误:在'&amp;'之前缺少',' 56 1
7-错误C2143:语法错误:缺少';'在'&amp;'之前40 1 8-错误C2143:语法错误:缺少';'在'&amp;'之前48 1
9-错误C2228:'。imaginary'的左边必须有class / struct / union 58 1
10-错误C2228:'。imaginary'的左边必须有class / struct / union 67 1
11-错误C2228:'.real'的左边必须有class / struct / union 58 1
12-错误C2228:'.real'的左边必须有class / struct / union 65 1
13-错误C2373:'Complex :: operator!=':重新定义;不同类型的修饰符48 1
14-错误C2373:'Complex :: operator ==':重新定义;不同类型的修饰符40 1
15-错误C2556:'int&amp; Complex :: operator!=(const Complex&amp;)const':重载函数的区别仅在于来自'const bool&amp; Complex :: operator!=的返回类型(const Complex&amp; ;)const'48 1
16-错误C2556:'int&amp; Complex :: operator ==(const Complex&amp;)const':重载函数的区别仅在于来自'const bool&amp; Complex :: operator ==的返回类型(const Complex&amp; ;)const'40 1
17-错误C2805:二元'运算符&gt;&gt;'参数太少了1 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
答案 0 :(得分:2)
您可以在下面看到所有代码:
#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;
}
答案 1 :(得分:0)
Istream和Ostream运算符需要在Complex类中声明为朋友公共成员,以便它们访问Complex类的私有成员。
friend 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;
};
friend 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
}
另一方面,您同时声明两种类型。比较将返回bool,因此我删除了Complex&amp;:
bool Complex::operator==(const Complex &right) const{
if ((real == right.real) && (imaginary == right.imaginary))
return true;
else
return false;
}
我在操作员中看到了一个错误!=,替换了&amp;&amp;通过||因为不同的复数可以具有相同的实部或虚部。
bool Complex::operator!=(const Complex &right) const{
if ((real != right.real) || (imaginary != right.imaginary))
return true;
else
return false;
}
这就是我所看到的,试着告诉我们......
答案 2 :(得分:0)
警告我不喜欢用...解决...
Complex Complex::operator*(const Complex &operando2) const
{
return **Complex**((real * operando2.real) - (real * operando2.imaginary), (real * operando2.imaginary) - (imaginary*operando2.real));
}