2012-04-21 64 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