#include"stdafx.h"
#include<iostream>
class Item
{
int exponent;
double coefficient; //two private members
public:
//constructor
Item(int exp = 0, int coef = 0) :exponent(exp), coefficient(coef) {}
Item(Item& item) {
exponent = item.exponent;
coefficient = item.coefficient;
} //copy constructor
//interface to change private member exponent
void change_exp(const int n) {
Item::exponent = n;
}
//interface to change private member coefficient
void change_coef(const double n) {
Item::coefficient = n;
}
int get_exp() { return exponent; }// interface to get exponent
double get_coef() { return coefficient; }// interface to get coefficient
~Item() {}
Item operator=(Item item2) {
change_exp(item2.get_exp());
change_coef(item2.get_coef());
return (*this);
}
Item operator*(Item & item2) {
Item result;
result.change_coef(coefficient * item2.get_coef());
result.change_exp(exponent + item2.get_exp());
return result;
}
};
int main() {
Item test(2, 7);
Item test2(4, 5);
Item result;
result = test * test2; //BUG!!!
return 0;
}
the bug appears on the line commented BUG
Error C2679 binary '=': no operator found which takes a right-hand operand of type 'Item' (or there is no acceptable conversion)
when I only assign one object like
result=test;
it is fine.
I dont know where went wrong in my = overloading function.
Please help......