我有以下代码
#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
如果我删除静态关键字,它编译得很好,但我在类定义中使用了这个静态关键字,所以如果我删除它,不会出错吗?
答案 0 :(得分: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));
}
答案 1 :(得分:1)
fromPolar
的替代方案。
可能你已经知道你不能这样写:
class complex
{
//..
explicit complex(double real, double imaginary);
explicit complex(double radius, double angle); //same as above
};
第二个构造函数与第一个构造函数基本相同(参数的不同名称没有任何区别),因此您无法执行此操作,这就是您提出fromPolar
解决方案的原因。
但即使在fromPolar
解决方案中,仍存在一个微妙的问题: angle 参数的单位是什么?它是弧度,程度还是什么?
所以我建议下面的课程解决上面讨论的两个问题,你不再需要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
};
希望让你的课程更好一点。