2011-04-18 90 views
0

所有的代碼工作正常,沒有錯誤,但下面的命令Setreal()Setimag() 給出錯誤的輸出。C級+的輸出錯誤


#include <iostream> 
using namespace std; 

class complex 
{ public: 
    bool Readcomplex() 
    { cout<<"Enter the real part"<<endl; 
     cin>>real; 
     cout<<"Enter the imaginary part"<<endl; 
     cin>>imag; 
     return true; }; 
    double Getreal() 
    { return real; 
      }; 
    double Getimag() 
    { return imag; 
      }; 
    double Add(complex c) 
    { real=real+c.real; 
     imag=imag+c.imag; 
      }; 
    double Setimag(double im) 
    { imag=im; 
      }; 
    double Setreal(double re) 
    { real=re; 
      }; 
    void Multiply(complex c) 
    { double x; 
     x=real*c.real-imag*c.imag; 
     imag=real*c.imag+imag*c.real; 
     real=x; 
      }; 
private: 
     double real; 
     double imag; 
    }; 
int main() 
{ complex A,B,E,R; 
double C,D; 
A.Readcomplex(); 
B.Readcomplex(); 
cout<<"The complex no. A is "<<A.Getreal()<<"+i"<<A.Getimag()<<endl; 
cout<<"The complex no. B is "<<B.Getreal()<<"+i"<<B.Getimag()<<endl; 
A.Add(B); //Result stored in A. 
cout<<"The complex no. A2 is "<<A.Getreal()<<"+i"<<A.Getimag()<<endl; 
cout<<"Set the real of A"<<endl; 
cin>>C; 
cout<<"and set the imaginary part"<<endl; 
cin>>D; 
cout<<"the new A is"<<A.Setreal(C)<<"+i"<<A.Setimag(D)<<endl; //WRONG OUTPUT 
A.Multiply(B); 
cout<<"The complex no. A is "<<A.Getreal()<<"+i"<<A.Getimag()<<endl; 
system("pause"); 
return 0;} 

錯誤的結果是在cout<<"the new A is"<<A.Setreal(C)<<"+i"<<A.Setimag(D)<<endl; //WRONG OUTPUT的結果是1.#QNAN+i1.#QNAN而不是像C+iD

回答

3

這些方法c和d的作爲值應該有一個return語句:

double Setimag(double im) 
{ 
    return imag=im; 
}; 
double Setreal(double re) 
{ 
    return real=re; 
}; 
+0

是。它現在正常工作,非常感謝GregC – 2011-04-18 04:14:13

+1

更少的擔心,更多的是,歡迎來到這個網站。 – GregC 2011-04-18 04:22:19

1
double Setreal(double re) 
{ 
    real=re; 
}; 

這個函數應該返回類型的double的東西,但它不...