计算方法

时间:2017-02-11 02:54:41

标签: c++ class complex-numbers

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:二进制'+':'复杂'未定义此运算符或转换为预定义运算符可接受的类型c:\ users \ Sarah \ desktop \ classwork \ c ++ \ exam 3 program \ exam 3 program \ exam 3 program.cpp 37

     

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

     

错误2错误C2784:'std :: _ St​​ring_const_iterator< _Elem,_Traits,_Alloc> std :: operator +(_ String_const_iterator< _Elem,_Traits,_Alloc> :: difference_type,std :: _ St​​ring_const_iterator< _Elem,_Traits,_Alloc>)':无法推断'std :: _ St​​ring_const_iterator< _Elem,_Traits,_Alloc>的模板参数“来自'Complex'c:\ users \ Sarah \ desktop \ classwork \ c ++ \ exam 3 program \ exam 3 program \ exam 3 program.cpp 37

1 个答案:

答案 0 :(得分:0)

function passWord() {
 var testV = 1;
 var pass1 = prompt('Please Enter Your Password',' ');
  while (testV < 3) {
   if (!pass1) 
   history.go(0);
   if (pass1.toLowerCase() == "letmein") {
   alert('You Got it Right!');
   window.open('/html/ok.html',"_self");
   break;
   } 
  testV+=1;
  var pass1 = 
  prompt('Access Denied - Password Incorrect, Please Try     Again.','Password');
 }
 if (pass1.toLowerCase()!="password" & testV ==3) 
 history.go(0);
 return " ";
}  

您正在尝试使用运营商&#39; +&#39;对于两个复杂实例。它没有定义。 变量sum也没有定义。 我猜你想要实现c = x + y或c.sum(x,y)。 然后这个函数应该像this.real = x.real + y.real; this.img = x.img + y.img

此外,你不能在函数调用中定义变量,如c.sum(Complex r,Complex i);