0

所有代码都正常工作,没有错误,但命令Setreal()Setimag() 以下给出错误输出。


#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 和 D 的值C+iD

4

2 回答 2

3

这些方法应该有一个返回语句:

double Setimag(double im)
{
  return imag=im;
};
double Setreal(double re)
{
  return real=re;
};
于 2011-04-18T04:11:27.520 回答
1
double Setreal(double re)
 { 
   real=re;
 };

这个函数应该返回某种类型的东西double,但它没有......

于 2011-04-18T04:12:26.613 回答