计算方法中的复数程序编译时错误 [英] Complex number program compile-time error at calculation methods
问题描述
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;
}
我完全不允许用户超载。
我相信我有几个错误,但是它们有一个共同的线程。它们出现在我方法中的计算点上。我不知道该如何解决。
I have several errors, but they have a common thread, I believe. They occur at the calculation points in my methods. I don't know how to fix it. I believe I have the public and private parts down, though.
这是我的错误:
错误7错误C2676:二进制 +:复杂未定义此运算符或未转换为预定义运算符可接受的类型c:cusers\Sarah\desktop\classwork\c ++ \考试3程序\考试3程序\考试3程序。cpp37
Error 7 error C2676: binary '+' : 'Complex' does not define this operator or a conversion to a type acceptable to the predefined operator c:\users\Sarah\desktop\classwork\c++\exam 3 program\exam 3 program\exam 3 program.cpp 37
错误16错误C3867:'Complex :: sum':函数调用缺少参数列表;使用'& Complex :: sum'创建指向成员c的指针:用户,Sarah,桌面,类,c ++,exam 3程序,exam 3程序,exam 3程序。cpp58
Error 16 error C3867: 'Complex::sum': function call missing argument list; use '&Complex::sum' to create a pointer to member c:\users\Sarah\desktop\classwork\c++\exam 3 program\exam 3 program\exam 3 program.cpp 58
错误2错误C2784:'std :: _ String_const_iterator< _Elem,_Traits,_Alloc> std :: operator +(_ String_const_iterator< _Elem,_Traits,_Alloc> :: difference_type,std :: _ String_const_iterator< _Elem,_Traits,_Alloc>)' 'from'Complex'c:\users\Sarah\desktop\classwork\c ++ \exam 3 program\exam 3 program\exam 3 program.cpp 37
Error 2 error C2784: 'std::_String_const_iterator<_Elem,_Traits,_Alloc> std::operator +(_String_const_iterator<_Elem,_Traits,_Alloc>::difference_type,std::_String_const_iterator<_Elem,_Traits,_Alloc>)' : could not deduce template argument for 'std::_String_const_iterator<_Elem,_Traits,_Alloc>' from 'Complex' c:\users\Sarah\desktop\classwork\c++\exam 3 program\exam 3 program\exam 3 program.cpp 37
推荐答案
void Complex::sum(Complex r, Complex i)
{
sum = r + i; //error here under "sum" and "+"
}
对两个复杂实例使用运算符 +。未定义。也未定义
变量和。
我可以猜到您想实现c = x + y或c.sum(x,y)。
然后,该函数应如下所示:real = x.real + y.real; this.img = x.img + y.img
you are trying to use operator '+' for two Complex instances. It is not defined. variable sum is not defined either. I can guess you wanted to implement c = x + y or c.sum(x,y). Then this function should look like this.real = x.real + y.real; this.img = x.img + y.img
同样,您不能在函数调用中定义变量,例如c.sum(Complex r,Complex i);
Also you cannot define variables inside function call like this c.sum(Complex r, Complex i);
这篇关于计算方法中的复数程序编译时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!