0
我在*運算符重載時遇到問題。當我想用雙這是給我下面的錯誤乘以oblect類運算符*超載
「極地極地::運算符*(const的極地& P,雙I)必須採取零個或一個參數」 我怎樣才能解決錯誤? 請指教
#include<iostream>
#include <cstdlib>
#include<complex>
using namespace std;
class Polar
{
float r; // distance
float q; // angle in degrees
public:
Polar (int dist, int angle)
{
r = dist;
q = angle;
}
Polar (int dist)
{
r = dist;
q = 0;
}
Polar (const Polar &P)
{
r = P.r;
q = P.q;
}
Polar()
{
r = 0;
q = 0;
}
~Polar()
{
}
int get_r(void)const
{
return r;
}
int get_q(void)const
{
return q;
}
Polar operator= (const Polar &p);
Polar operator* (const Polar &p);
Polar operator* (const Polar &p, double i);
Polar operator* (double i ,const Polar &p);
void print (int i) const
{
cout<< "P"<<i<<"("<<r<<","<< q <<")"<<endl<<endl;
}
};
inline Polar Polar::operator= (const Polar &p)
{
r = p . r;
q = p . q;
return*this;
}
inline Polar Polar::operator* (const Polar &p)
{
Polar temp;
temp.r = r * p.r;
temp.q = q + p.q;
return temp;
}
inline Polar Polar::operator* (const Polar &p, double i)
{ Polar temp;
temp.r = p.r *i;
temp.q = p.q;
return temp;
}
inline Polar Polar::operator* (double i ,const Polar &p)
{ Polar temp;
temp.r = i * p.r;
temp.q = p.q;
return temp;
}
int main ()
{
Polar P1, P2(1), P3(1, 90), P4(2, 45);
P1.print(1);
P2.print(2);
P3.print(3);
P4.print(4);
Polar P5(P4);
P5.print (5);
Polar P6 = P2 * Polar (5, 90) ;
Polar P7 = P2*P3*P4;
P6.print (6);
P7.print (7);
Polar P8 = P4 * 3.0;
P8.print(8);
Polar P9 = 7.0 * P4;
P9.print(9);
return 0;
}