复数程序编译时的计算方法错误
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;
}
我根本不允许用户重载
我有几个错误,但我相信它们有一个共同的线索。它们出现在我的方法中的计算点上。我不知道该怎么解决。不过,我相信我已经把公共和私人部分搞砸了。
以下是我的错误:
错误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<_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);
- 是否有一种标准方法来计算两个 asctime() 值之间的天数
- 有没有一种惯用的方法可以在不存储变换或不必要地重新计算的情况下找到数组变换的最小/最大值?
- 寻求提高Microsoft密封库计算效率的方法
- 在 C++20 计算范围内相邻对的最简洁明了的方法是什么?
- 计算浮点余数的最佳方法
- 计算将整数n分解为4平方和的方法的数量
- 计算两个uint8_t变量之间差值的最快方法是什么?
- 使用 MC 方法计算积分
- 计算 <Classtype*> 向量中所有项的布尔值的最有效方法,如果全部为真则返回 true
- C++ std::string 中是否有任何函数可以计算两个字符串的相同起始字符的总数或任何最佳方法
- C/C++ 方法中最快/最短计算二进制数字总和/又名二进制中的 1 数
- 对于我扩展此程序来计算最高10x10矩阵的倒数的最简单方法是什么
- 什么是计算C中PI的最有效方法
- 是否有一种计算CGAL中Surface_mesh对象的PCA的方法
- 是否有任何数据类型或方法可以计算当前单元格中先前数组单元格的总和
- 有没有一种简单的方法来计算库中记录的函数
- 复数程序编译时的计算方法错误
- 数据包CRC计算方法
- C++中错误的计算方法
- 编译器本身使用并行计算方法运行