我有以下代码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