0

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?

kosmsamo
  • 11
  • 2
  • 1
    Your operator overload would apply if you were sorting a vector of TaxPayers, but a TaxPayer* is not the same as a TaxPayer. You can use a custom comparison function. – user253751 Jun 14 '17 at 00:06
  • Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ Jun 14 '17 at 00:12
  • Seem that you can not use pointer as input of comparison function, https://stackoverflow.com/questions/3871039/how-to-overload-operator-for-a-pointer-to-the-class . And, by doing this, you're running the function `calculateTotalTax` multiple time. – Hải Phạm Lê Jun 14 '17 at 00:15
  • 1
    @HảiPhạmLê Huh? Of course you can provide a custom comparison function using pointers. – πάντα ῥεῖ Jun 14 '17 at 00:21
  • I tried this on MSVC: `bool operator<(A * lhs, A * rhs)`, it causes compile error : `error C2803: 'operator <' must have at least one formal parameter of class type`. And in the question i posted, seem that we can not custom operator for pointers. – Hải Phạm Lê Jun 14 '17 at 00:56
  • @HảiPhạmLê Yes but that's not what is meant by "a custom comparison function". – user253751 Jun 14 '17 at 01:10
  • @immibis Oh, i got it, you mean the third parameter in `sort` function, right ? Yes, should provide a custom function there to compare 2 entry of the vector. – Hải Phạm Lê Jun 14 '17 at 01:47

0 Answers0