Below are three pieces of code, main.cpp, my header file, and CarDealer.cpp. The question I have is on overloading the -- operator. My first problem is that when I write yourCar-- it just subtracts it off right away and doesn't work as it is supposed to (I'm having struggling with how to define decrement in CarDealer.cpp). My second problem is that when I try to print cout << yourCar-- << endl; it prints a huge error and I don't know why. Any help would be much appreciated!
bool CarDealer::operator>(CarDealer& car)
{
if(this->GetMaxDistance() > car.GetMaxDistance())
{
cout << "The first car has traveled more" << endl;
};
if(car.GetMaxDistance() > this->GetMaxDistance())
{
cout << "The second car has traveled more" << endl;
};
if(car.GetMaxDistance() == this->GetMaxDistance())
{
cout << "Both cars have travled the same amount" << endl;
}
return false;
}
What do you mean by subtracting it off right away? What error are you getting? And you should note that your decrement function doesn't modify MaxDistance ever; i initially is 0, and it's multiplied to the result of the formula, making g = 0.
Sorry if I wasn't clear. What I meant to say is that when I run the main function I get values of 200, 160, 120 or something like that. The yourCar which we are subtracting starts off at 240. So because it is a postfix operator I should be getting values of 240, 200, 160 when I print the yourCar values and like you mentioned it has something to do with the decrement function. Also I am getting an error when I try printing cout << yourCar-- << endl; but when I just do yourCar--; and then cout << yourCar << endl; it works.
ok I understand now, its hard to say the error in this case though because its prints hundreds of lines of error when I try to compile, at one part at the end it says no known conversion for argument 2 from 'CarDealer' to 'CarDealer&'