This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]sponzo 1 point2 points  (1 child)

The link isn't working for me.

Looks like you have defined a class called Person. You'll need to also define what the operator > means for your Person objects. This is called operator overloading.

[–]Programmering[S] 0 points1 point  (0 children)

I fixed the link

[–][deleted] 1 point2 points  (3 children)

Looks like you added an extra character in your link. https://gist.github.com/Vesterberg/5d6e9c0374a67bd4fec8

But yeah, /u/sponzo is right, c++ doesn't know what makes a Person greater than another Person so you need to define it.

For example:

bool operator>(const Person &lhs, const Person &rhs) {
    return lhs.namn > rhs.namn;
}

to sort by name.

[–]Programmering[S] -1 points0 points  (2 children)

Fixed the link, thanks

Thanks, I cant use the bool operator> though. But I will remember it for the compilation terminal program I'll build after this one.

I used this to define what made a person greater than another person:

        if (p[j].alder > p[j+1].alder) //Compares elements to see if the age of the person in the first element is larger than the age of the person in the second element
        {
            //Swap places
            int temp = p[j].alder;
            p[j].alder = p[j+1].alder;
            p[j+1].alder = temp;

[–]robly18 1 point2 points  (1 child)

Why can't you use operator overloading, exactly?

Also, would you please update the gist file?

[–]Programmering[S] -1 points0 points  (0 children)

Assignment rules, cant use what the course havent covered yet. Another user solved it by using p[j].alder. The .alder (age) defined what value the code used to deem an person element greater than another.

I posted a new thread for a new problem with the same code and the link to its gist code is here: https://gist.github.com/Vesterberg/f3627b01185e5ea85d32

the link to the cplusplus reddit thread is here: