I am creating a program whose basic function is to calculate the total tax of a tax return. Among other things, I want to sort a vector which contains these tax returns.
I have this class:
// Necessary includes
class TaxPayer :public Person {
private:
// Some variables
std::vector<Property *> properties;
public:
// Rest of the code
double TaxPayer::calculateTotalTax() const
{
double sum = 0.0;
for (int i = 0; i < (int)properties.size(); i++) {
// calculateTax is a virtual function of Property class.
// It is defined by the classes derived from Property.
// It returns a double (the result of a formula).
sum += properties[i]->calculateTax();
}
return sum;
}
bool TaxPayer::operator<(TaxPayer const & m_TaxPayer)
{
return this->calculateTotalTax() < m_TaxPayer.calculateTotalTax();
}
};
My main program looks like this:
// Necessary includes
vector<TaxPayer *> sortByTotalTax(vector<TaxPayer *> notSortedVector);
int main() {
vector<TaxPayer *> vectorPayer, sortedVectorPayer;
// Some code
sortedVectorPayer = sortByTotalTax(vectorPayer);
return 0;
}
// No third parameter. I am using the default one ( < ).
// That's why I overloaded the operator < in my TaxPayer class.
vector<TaxPayer *> sortByTotalTax(vector<TaxPayer *> notSortedVector) {
sort(notSortedVector.begin(), notSortedVector.end());
return notSortedVector;
}
My program does not sort the vector as intended. I assume there is a mistake in overloading the operator <.
Any suggestions?