0

我有以下代码

#include<math.h>
class complex
{

public:
    double getRe();
    double gerIm();
    void setRe(double value);
    void setIm(double value);
    explicit complex(double=0.0,double=0.0);
    static complex fromPolar(double radius,double angle);
    complex operator+(complex rhs);
    complex operator-(complex rhhs);
    complex operator*(complex rhs);
    complex operator+(double rhs);
    complex operator-(double rhs);
    complex operator*(double rhs);
    complex conjugate();
    double norm();
    complex operator/(double rhs);
    complex operator/(complex rhs);

private:
    double real;
    double img;

};
complex operator+(double lhs,complex rhs);
complex operator-(double lhs,complex rhs);
complex operator*(double lhs,complex rhs);
complex operator/(double lhs,complex rhs);
complex exp(complex c);
inline double complex::getRe(){return real;}
inline double complex::gerIm(){ return img;}
inline void complex::setRe(double value) {  real=value;}
inline void complex::setIm(double value) { img=value;}
 inline complex::complex(double re,double im) :real(re),img(im){}
 inline static  complex complex::fromPolar(double radius,double angle){

     return complex(radius*cos(angle),radius*sin(angle));

 }
 inline complex complex::operator+(complex rhs)
 {
     return complex(this->real+rhs.real,this->img+rhs.img);

 }
 inline complex complex::operator-(complex rhs)
 {
     return complex(this->real-rhs.real,this->img-rhs.img);

 }
 inline complex complex::operator*(complex rhs)
 {
     return complex(this->real*rhs.real-this->img*rhs.img,this->real*rhs.img+this->img*rhs.real);

 }
 inline complex complex::operator+(double rhs)
 {
     return complex(this->real+rhs,this->img);

 }

 inline complex complex::operator-(double rhs)
 {
     return complex(this->real-rhs,this->img);

 }
 inline complex complex::operator*(double rhs)
 {
     return complex(this->real*rhs,this->img*rhs);

 }
 inline complex complex::operator/(double rhs)
 {
     return complex(this->real/rhs,this->img/rhs);

 }
 inline complex complex::operator/(complex rhs)
 {

     return (*this)*rhs.conjugate()/rhs.norm();


 }

 inline double complex::norm()
 {
 return (this->real*this->real+this->img*this->img);
 }

 inline complex complex::conjugate()
 {

     return complex(this->real,-this->img);
 }


 inline complex operator+(double lhs,complex rhs)
 {
     return rhs+lhs;
 }

 inline complex operator-(double lhs,complex rhs)
 {
     return complex(lhs-rhs.getRe(),rhs.gerIm());

 }
 inline complex operator*(double lhs,complex rhs)
 {
     rhs*lhs;

 }

 inline complex operator/(double lhs,complex rhs)
 {
     return rhs.conjugate()*lhs/rhs.norm();

 }

但据说

1>c:\users\daviti\documents\visual studio 2010\projects\complex_number\complex_number\complex.h(38): error C2724: 'complex::fromPolar' : 'static' should not be used on member functions defined at file scope

如果我删除 static 关键字,它编译得很好,但是我在类定义中使用了这个 static 关键字,所以如果我删除它,不会出错吗?

4

2 回答 2

5

static唯一需要出现在类定义中,而不是你实现方法的地方。

class complex
{
    //......
    static complex fromPolar(double radius,double angle);
    //.....
};

inline complex complex::fromPolar(double radius,double angle){
     return complex(radius*cos(angle),radius*sin(angle));
}
于 2012-04-21T09:55:43.367 回答
1

@Luchian 已经很好地回答了这个问题。我将讨论fromPolar.

可能你已经知道你不能这样写:

class complex
{
    //..
    explicit complex(double real, double imaginary);
    explicit complex(double radius, double angle); //same as above
};

第二个构造函数与第一个构造函数基本相同(参数的不同名称没有任何区别),所以你不能这样做,这就是你想出fromPolar解决方案的原因。

但是即使在解决方案中仍然存在一个微妙的问题:角度参数fromPolar的单位是什么?它是弧度,度数还是什么?

所以我会提出以下课程,它解决了上面讨论的两个问题,你不再需要fromPolar函数了。

class complex
{
    //..
    explicit complex(double real, double imaginary);
    explicit complex(double radius, Angle angle); //now it is different
};

另一个类定义Angle为:

class Angle 
{
  double m_value; //always maintain this in radian

 public:
  Angle(double value, bool isradian = true);

  double radian(){ return m_value; }
  double degree(){ return m_value * 180 / PI; }//or maybe M_PI is the symbol
};

希望这能让你的课好一点。

于 2012-04-21T10:24:05.903 回答