我在尝试编译以下代码时遇到错误。我是初学者。请帮我搞清楚。我试图重载运算符_,+,/,*。它在编译时显示错误。我应该使用"朋友"解决这个问题?
#include<stdio.h>
class complex{
private:
double real; //real part of complex
double imag; // imaginary part of complex
public:
complex(double r=0., double i=0.):real(r),imag(i)
{
} // constructor with initialization
complex(const complex&c):real(c.real),imag(c.imag)
{
} // copy constructor with initialization
~complex()
{
} // destructor
double re() const
{
return real;
} // read real part
double im() const
{
return imag;
} // read imaginary part
const complex& operator=(const complex&c)
{
real=c.real;
imag=c.imag;
return *this;
} //assignment operator
const complex& operator+=(const complex&c)
{
real += c.real;
imag += c.imag;
return *this;
} // addition of current complex
const complex& operator-=(const complex&c)
{
real -= c.real;
imag -= c.imag;
return *this;
} // subtract from current complex
const complex& operator*=(const complex&c)
{
double keepreal = real;
real = real*c.real-imag*c.imag;
imag = keepreal*c.imag+imag*c.real;
return *this;
} // multiply current complex with a complex
const complex& operator/=(double d)
{
real /= d;
imag /= d;
return *this;
} // divide current complex with real
void print(const complex&c)
{
printf("(%f,%f)\n",c.re(),c.im() );
} // printing complex number
friend complex operator !(const complex& c)
{
return complex(c.re(),-c.im());
}
friend double abs2(const complex& c)
{
return c.re()*c.re()+c.im()*c.im();
} // absolute value of complex
const complex& operator/=(const complex&c)
{
return *this *= (!c)/=abs2(c);
} // divide the current complex by a complex
const complex operator-(const complex& c)
{
return complex(-c.re(),-c.im());
} // negative of complex number
const complex operator-(const complex& c,const complex& d)
{
return complex(c.re()-d.re(),c.im()-d.im());
} // difference between complex numbers
const complex operator+(const complex& c,const complex& d)
{
return complex(c.re()+d.re(),c.im()+d.im());
} // addition of complex numbers
const complex operator*(const complex& c,const complex& d)
{
return complex(c)*=d;
} // multiplication of complex numbers
const complex operator/(const complex& c,const complex& d)
{
return complex(c)/=d;
} // division of complex numbers
};
int main(){
complex c(1.,0.),d(3.,4.);
print(c-d);
print(c/d);
return 0;
}
我得到的输出是:
complex_nums.cpp:76:59: error: ‘const complex complex::operator-(const complex&, const complex&)’ must take either zero or one argument
const complex operator-(const complex& c,const complex& d)
^
complex_nums.cpp:80:59: error: ‘const complex complex::operator+(const complex&, const complex&)’ must take either zero or one argument
const complex operator+(const complex& c,const complex& d)
^
complex_nums.cpp:84:59: error: ‘const complex complex::operator*(const complex&, const complex&)’ must take either zero or one argument
const complex operator*(const complex& c,const complex& d)
^
complex_nums.cpp:88:59: error: ‘const complex complex::operator/(const complex&, const complex&)’ must take exactly one argument
const complex operator/(const complex& c,const complex& d)
^
complex_nums.cpp: In function ‘int main()’:
complex_nums.cpp:96:11: error: ‘print’ was not declared in this scope
print(c-d);
^
complex_nums.cpp:97:9: error: no match for ‘operator/’ (operand types are ‘complex’ and ‘complex’)
print(c/d);
答案 0 :(得分:2)
所有运算符(AppTheme
)只需要一个或不需要参数,除非它们是友元函数。将所有<item name="android:fastScrollTextColor">@color/apptheme_color</item> //this is used for the color of the Alphabetical Fast scrollView
<item name="android:fastScrollPreviewBackgroundRight">@drawable/bg_default_focused_holo_light</item> //this is the image or and drawable file you want to set on Alphabetical Fast scrollView
函数标记为+,-,*,/
,代码应该有效。否则,从每个运算符中消除1个参数,而不是使用当前实例(operator
)。已删除参数的friend
运算符示例:
this
你在使用const complex operator+(const complex& c)
{
return complex(c.re()+re(),c.im()+im());
}
功能做什么?它应该在全局范围内,因为它需要friend
作为参数。将print()
移出全局范围,就像这样。如果您仍想为对象本身保留complex
,请执行此操作,但您的课程应如下所示:
print()
答案 1 :(得分:1)
二元运算符是带有2个参数的自由函数(首选)或带有一个参数的成员函数(不太好)。
您已使用2个参数定义了成员函数。
通过添加friend
将其转换为免费功能是一种方式。
另一种方法是让它们成为自由函数,在类之外定义,但是按成员函数编写(正如你所做的那样):
struct complex
{
/* ... */
};
// class definition ends
//free function
inline complex operator/(const complex& c,const complex& d)
{
return complex(c)/=d;
} // division of complex numbers
注意,这个功能可以改进:
inline complex operator/(complex c,const complex& d)
{
c /= d;
return c;
}
此外,一元运算符应返回complex&
,而不是const complex&
。你返回的东西是可变的,因为你只是改变了它。
答案 2 :(得分:0)
对于运算符must take either zero or one argument
的错误,这是因为您将非成员函数实现为成员函数。这些应该是自由函数才能正常工作,或者只应该使用一个参数并使用this
作为另一个参数。
请参阅此问题以澄清:
Operator overloading : member function vs. non-member function?
对于打印错误,我猜您正在尝试调用打印成员函数。要做到这一点,你需要做,例如以下内容:
complex(c-d).print();