2

I know this question has been asked before in similar circumstances, however I am new to C++ and cannot figure out what needs to be done or why it has to be done. Basically I am using a Person object that I created which has the variables name, age, height, and weight. I need to run a Quick Sort on the Person objects that I have created and sort them by age.

Person Class:

class Person{
private:
    string name;
    int age, height, weight;
public:
Person(string name = "empty", int age = 0, int height = 0, int weight = 0)
{
    this->name = name;
    this->age = age;
    this->height = height;
    this->weight = weight;
};

Quick Sort Method:

// Quick Sort
void QuickSort(Person *A, int start, int end){
    if(start < end){
        int p = partition(A, start, end);
        QuickSort(A, start, p - 1);
        QuickSort(A, p + 1, end);
    }
}
int partition(Person *A, int start, int end){
    Person pivot = A[end];
    int p = start;
    for(int i = start; i <= end - 1; i++){
        if(A[i] <= pivot){
            Person temp = A[i]; A[i] = A[p]; A[p] = temp;
            p++;
        }
    }
    Person temp = A[end]; A[end] = A[p]; A[p] = temp;
    return p;
}

I am getting the error "Invalid operands to binary expression ('Person' and 'Person) on the line:

if(A[i] <= pivot)

Any help would be appreciated because I have tried multiple things and I have looked it up but cannot get other suggestions to work in this situation. I would appreciate an explanation as to why I was getting this error too if possible. Thanks!

Fall0ut
  • 71
  • 1
  • 12
  • Can you post the actual error message? Also, your code is not complete. As it is, this question has no [mcve] – Justin Feb 14 '18 at 22:50
  • @Justin That is the exact error message I am getting. "Invalid operands to binary expression ('Person' and 'Person')." Its not all of my code but the main method will not effect this result. – Fall0ut Feb 14 '18 at 22:53

2 Answers2

5

C++ doesn't automatically know how to perform a comparison on class Person.

If you just want to sort by age, try

if(A[i].age <= pivot.age)

If you want to learn about defining special operators so that "Person <= Person" will work automatically, check out cppreference.com: http://en.cppreference.com/w/cpp/language/operators

Ian Neal
  • 113
  • 5
  • Yes! This worked! Can you explain to me what the difference is between -> and . pointers? I am coming from Java and when I transferred had to learn C++. I cannot understand when to use which. – Fall0ut Feb 14 '18 at 22:54
  • 2
    @Fall0ut Normally you use `.` when the left side is not a pointer and `->` when the left side is a pointer. – HolyBlackCat Feb 14 '18 at 22:56
  • @HolyBlackCat okay thank you. Much appreciation everyone! – Fall0ut Feb 14 '18 at 22:57
  • 1
    Yes: To access a member of an object, you use '.' like pivot.age. "pivot" is not a pointer, since pivot is type "Person". However, if you were to define "Person *ptr", ptr would be a pointer. You can access pointers by 1) dereferencing them (example: pivot = *ptr) then 2) accessing their members (pivot.age). '->' is a shorthand for this (ptr->age). – Ian Neal Feb 14 '18 at 22:58
  • 1
    Also check out https://stackoverflow.com/questions/1238613/what-is-the-difference-between-the-dot-operator-and-in-c – Ian Neal Feb 14 '18 at 22:58
  • thanks everyone. One more question. I need setter/getter for age since it is private. This is not required and I did get my program to work, but I know it is better practice, and as a student I want to do the best I can. I would obviously just do Person.setAge(foo) <= pivot.setAge(foo). What would be in place of foo in order to get it to work? – Fall0ut Feb 14 '18 at 23:23
  • 1
    A side comment: A good setter does more than just set a value. It provides some value-added behaviour like protecting your object from some fool trying to set a bad value. If you have a setter that does no checking and blindly sets, all it is really good for is a place to hang a breakpoint when debugging. If you have a `public` getter and a stupid, trusting `public` setter, your member is just as exposed as if it is `public`. – user4581301 Feb 14 '18 at 23:30
  • my god the C++ community is a hell of a lot more helpful. Usually in Java i ridiculed and here everyone is giving great info. I really appreciate it guys and Im loving this language a lot – Fall0ut Feb 14 '18 at 23:33
  • 1
    No, we're often just as bad. You caught us at a good time. Take the advice and look into overloading `<=`. Good reading on the topic: [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading). – user4581301 Feb 14 '18 at 23:37
  • Another note: Most sort algorithms are based around `<` rather than `<=` because this solves the sorts of problems you can get into (like infinite loops) when you do have two equal items. Do a web search for "strict weak ordering" for more on this topic. – user4581301 Feb 14 '18 at 23:40
3

You said you want to sort by age, so you need to compare their ages:

if (A[i].age <= pivot.age)

Your code gets an error because you haven't defined a <= operator for your Person class.

Barmar
  • 741,623
  • 53
  • 500
  • 612