复数程序编译时的计算方法错误

Complex number program compile-time error at calculation methods

本文关键字:计算 方法 错误 程序 编译      更新时间:2023-10-16
class Complex
{
public:
Complex(float = 0.0, float = 0.0); //default constructor that uses default arg. in case no init. are in main
void getComplex(); //get real and imaginary numbers from keyboard
void sum(Complex, Complex); //method to add two complex numbers together
void diff(Complex, Complex); //method to find the difference of two complex numbers
void prod(Complex, Complex); //method to find the product of two complex numbers
void square(Complex, Complex); //method to change each complex number to its square
void printComplex(); //print sum, diff, prod, square and "a+bi" form 
private: 
float real; //float data member for real number (to be entered in by user)
float imaginary; //float data member for imaginary number (to be entered in by user)
};
Complex::Complex(float r, float i)
{   
real = r;
imaginary = i;
}
void Complex::sum(Complex r, Complex i)
{
sum = r + i; //error here under "sum" and "+"
}
int main()
{
Complex c;
c.getComplex();
c.sum(Complex r, Complex i); //error here under "Complex r"
c.diff(Complex r, Complex i); //error here under "Complex r"
c.prod(Complex r, Complex i); //error here under "Complex r"
c.square(Complex r, Complex i); //error here under "Complex r"
c.printComplex();
return 0;
}

我根本不允许用户重载

我有几个错误,但我相信它们有一个共同的线索。它们出现在我的方法中的计算点上。我不知道该怎么解决。不过,我相信我已经把公共和私人部分搞砸了。

以下是我的错误:

错误7错误C2676:二进制"+":"Complex"没有定义此运算符,也没有定义到预定义运算符c:\users\Sarah\desktop\classwork\c++\exation 3 program\exact 3 program.cpp 37 可接受的类型的转换

错误16错误C3867:"Complex::sum":函数调用缺少参数列表;使用'&Complex::sum'创建一个指向成员c:\users\Sarah\datadesk\classwork\c++\exation 3 program\exact 3 program.cpp 58 的指针

错误2错误C2784:"std::_String_const_iterator&lt_Elem、_Traits、_Alloc>std::运算符+(_String_const_iterator<_Elem,_Traits,_Alloc>:difference_type,std::_String_nst_iterator<_Elems,_Trait,_Alloc>)':无法推导"std::_String_const_iterator<"的模板参数_Elem,_Traits,_Alloc>'来自"Complex"c:\users\Sarah\桌面\课堂作业\c++\考试3程序\考试3计划.cpp 37

void Complex::sum(Complex r, Complex i)
{
sum = r + i; //error here under "sum" and "+"
}

您正试图对两个Complex实例使用运算符"+"。它没有定义。变量sum也没有定义。我猜您想要实现c=x+y或c.sum(x,y)。那么这个函数应该是这样的。real=x.real+y.real;this.img=x.img+y.img

此外,您不能在函数调用中定义这样的变量c.sum(复数r,复数i);