我有以下代碼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關鍵字類定義,所以如果我刪除它,不會是錯誤?
請使用標題爲''的'std :: complex '。在某些情況下,您的部門可能會超出/不流通。 –
2012-04-21 10:05:13
在這種情況下?例如當除數爲0時? – 2012-04-21 10:07:43
當兩個操作數都是例如。大約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