2012-04-21 122 views
0

我有以下代码static关键字错误

#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关键字类定义,所以如果我删除它,不会是错误?

+1

请使用标题为''的'std :: complex '。在某些情况下,您的部门可能会超出/不流通。 – 2012-04-21 10:05:13

+0

在这种情况下?例如当除数为0时? – 2012-04-21 10:07:43

+1

当两个操作数都是例如。大约10^200(或10^-200),结果是明确的,但是你的方法溢出。见例如。 http://www.mpi-hd.mpg.de/astrophysik/HEA/internal/Numerical_Recipes/f5-4.pdf。如果计算模量(如果| | | | | sqrt(1 +(y/x)^ 2)/y)^ 2)'else代替普通的'sqrt(x^2 + y^2)')。作为一般规则,当标准库中有一个完美的数字类时,不要自己编写复杂的数字类(也适用于矩阵)。 – 2012-04-21 10:15:26

回答

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)); 
} 
+0

好吧,我会考虑下一次 – 2012-04-21 10:03:08

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 
}; 

希望。

+0

BU我需要构造函数的角度是每次,我叫m_value作为参数对吗? – 2012-04-21 10:32:59

+0

@dato:不,'m_value'是'private',所以你不能使用它。你必须使用'angle.radian()',或者如果你需要度数角度(无论出于何种原因),你可以调用'angle.degree()'。 – Nawaz 2012-04-21 10:35:14

+0

但我必须声明像角度(双值)的构造函数:m_value(value){}是吗? – 2012-04-21 11:08:25