I keep receiving this error when trying to overload the * operator to multiply an object by an integer:
" error C2677: binary '*' : no global operator defined which takes type 'class percent' (or there is no acceptable conversion)"
Here is my code:
Code:
#include <iostream>
class percent
{
private:
int whole;
int decimal;
public:
percent();
percent(float z);
int getwhole(void);
int getdecimal(void);
void setwhole(int z);
void setdecimal(int z);
float getpercent(void) const;
void percent::setpercent(float z);
void print(void);
percent operator+(const percent& z) const; //e = f + g;
percent operator-(const percent& z) const; //e = f - g;
percent operator*(const percent& z) const; //e = f * g;
percent operator/(const percent& z) const; //e = f / g;
percent operator*(int z) const; //c = a * f;
friend std::ostream& operator<<(std::ostream& y, const percent& z);
friend std::istream& operator>>(std::istream& y, percent& z);
//c = a / f;
//c = c * f;
//c = c / f;
//c = f * a;
//c = a / f;
//c = f * c;
//c = f / c;
//e = f;
//e++;
//++e;
//e--;
//--e;
//if (e == f)
//if (e != f)
//if (e > f)
//if (e >= f)
//if (e < f)
//if (e <= f)
//cin >> e;
//cout << e;
};
using namespace std;
percent::percent()
{
whole = 0;
decimal = 0;
}
percent::percent(float z)
{
whole = z;
decimal = (z - whole) * 100;
}
int percent::getwhole(void)
{
return whole;
}
int percent::getdecimal(void)
{
return decimal;
}
void percent::setwhole(int z)
{
whole = z;
}
void percent::setdecimal(int z)
{
while (z >= 100)
{
z = z - 100;
whole++;
}
decimal = z;
}
float percent::getpercent(void) const
{
return whole + (decimal) / (100.0);
}
void percent::setpercent(float z)
{
whole = z;
decimal = (z - whole) * 100;
}
void percent::print(void)
{
cout << "Whole: " << whole << endl;
cout << "Decimal " << decimal << endl;
}
percent percent::operator+(const percent& z) const
{
percent temp;
temp.setwhole(whole + z.whole);
temp.setdecimal(decimal + z.decimal);
return temp;
}
percent percent::operator-(const percent& z) const
{
float a, b, c;
percent temp;
a = getpercent();
b = z.getpercent();
c = a - b;
temp.setpercent(c);
return temp;
}
percent percent::operator*(const percent& z) const
{
float a, b, c;
percent temp;
a = getpercent();
b = z.getpercent();
c = a * b;
temp.setpercent(c);
return temp;
}
percent percent::operator/(const percent& z) const
{
float a, b, c;
percent temp;
a = getpercent();
b = z.getpercent();
c = a / b;
temp.setpercent(c);
return temp;
}
percent percent::operator*(int z) const
{
float a, b;
percent temp;
a = getpercent();
b = a * z;
temp.setpercent(b);
return temp;
}
ostream& operator<<(ostream& y, const percent& z)
{
y << z.whole << "." << z.decimal << "%";
return y;
};
istream& operator>>(istream& y, percent& z)
{
float temp;
y >> temp;
z.setpercent(temp);
return y;
};
int main()
{
int a = 0, b = 0;
float c = 0, d = 0;
percent f(6);
percent e(20);
percent g(5);
//e.print();
c = a * f;
cout << c << endl;
return 0;
}
Any ideas on how to fix?
Thanks.