Overloading the -- operator

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!


#include <iostream> //main.cpp
#include <string>
#include "CarDealer.h"

using namespace std;

int main(void)
{
CarDealer myCar(" "," "," ",0);
CarDealer yourCar(" "," ", " ",0);

myCar.SetPlateNumber("program");
myCar.SetMake("Sweet");
myCar.SetModel("Cool");
myCar.SetGasMileage(20);
myCar.SetMaxDistance(0);
myCar.GasStation(2);
yourCar.SetPlateNumber("what");
yourCar.SetMake("Nice");
yourCar.SetModel("Awesome");
yourCar.SetGasMileage(20);
yourCar.SetMaxDistance(0);
yourCar.GasStation(11);

cout << myCar << endl;
cout << yourCar << endl;
yourCar--;
cout << yourCar << endl;
yourCar--;
cout << yourCar << endl;

if(myCar > yourCar)
{
cout << "My car wins." << endl;
}

return 0;
}


#ifndef CarDealer_h //CarDealer_H header
#define CarDealer_h
#include <string>

using namespace std;
class CarDealer
{
public:
CarDealer( string newPlateNumber, string newMake, string newModel, double newGasMileage);
friend ostream& operator<<(ostream& stream, CarDealer& cn );
void operator--();
CarDealer operator--(int dummy);
bool operator>(CarDealer& car);
string GetPlateNumber();
void SetPlateNumber(string str);
string GetMake();
void SetMake(string str);
string GetModel();
void SetModel(string str);
double GetGasMileage();
void SetGasMileage(float n);
double GetMaxDistance();
void SetMaxDistance(float n);
double GasStation(double dNumberofGallons);

private:
string PlateNumber;
string Make;
string Model;
double GasMileage;
double MaxDistance;
void decrement();
};

#endif //CarDealer_H


#include <iostream> //CarDealer.cpp
#include "CarDealer.h"
#include <string>

using namespace std;

CarDealer::CarDealer( string newPlateNumber, string newMake, string newModel, double newGasMileage )
{
PlateNumber = newPlateNumber;
Make = newMake;
Model = newModel;
GasMileage = newGasMileage;

}

void CarDealer::SetPlateNumber(string str)
{
this->PlateNumber = str;
}

string CarDealer::GetPlateNumber()
{
return this->PlateNumber;

}

void CarDealer::SetMake(string str)
{
this->Make = str;
}

string CarDealer::GetMake()
{
return this->Make;
}

void CarDealer::SetModel(string str)
{
this->Model = str;
}

string CarDealer::GetModel()
{
return this->Model;
}

void CarDealer::SetGasMileage(float n)
{
this->GasMileage = n;

if(GasMileage < 0)
{
cout << "That is not possible" << endl;
}
}

double CarDealer::GetGasMileage()
{
return this->GasMileage;
}

double CarDealer::GasStation(double dNumberofGallons)
{
this->MaxDistance = this->GasMileage * dNumberofGallons;

return this->MaxDistance;
}

void CarDealer::SetMaxDistance(float n)
{
this->MaxDistance = n;
}

double CarDealer::GetMaxDistance()
{
return this->MaxDistance;
}

void CarDealer::decrement()
{
int i=0;
int g = this->GasMileage*3*i--;
MaxDistance = MaxDistance - g;
}

CarDealer CarDealer::operator--(int dummy) //postfix ++ operator
{
CarDealer temp( GetMake(), GetModel(), GetPlateNumber(), GetMaxDistance() );
this->decrement();
return temp;
}

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;
}


ostream& operator<<( ostream& stream, CarDealer& cn )
{
stream << cn.GetMake() << " - " << cn.GetModel() << " - " << cn.GetPlateNumber() << " (Total Dist:" << cn.GetMaxDistance() << ")";

return stream;
}
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.
You can disregard my first question, I'm just being stupid. However I'm still getting an error when I try and execute this line

cout << yourCar-- << endl;
or
cout << "YourCar--" << endl;
> I'm still getting an error
...  2 >&1 | grep error
I'm sorry I don't follow what is grep error?
http://www.cplusplus.com/forum/articles/40071/#msg216270
> it prints a huge error
search for the line that says `error' (that's what grep is for)
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&'
ok so I got rid of the & sign for my << overloaded operator but now it prints a big number 1.7 *10^131
Topic archived. No new replies allowed.