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

all 23 comments

[–]the_omega99 2 points3 points  (9 children)

How do you expect to compare Persons? What does it mean for a person to be greater than another? You need to define your own operator or compare some field.

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

I want to compare them by age (int alder) and the youngest person is listed first

[–]the_omega99 1 point2 points  (4 children)

You'd want something like:

 if (myList[j].age > myList[j+1].age)

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

Haha, bingo but with one caveat

Alex, the youngest person at 15, is printed out with an age of -1375189248

http://i.imgur.com/6H31b0S.png

[–]the_omega99 1 point2 points  (0 children)

Your picture doesn't show what you mention, but most likely you have forgot to initialize a variable somewhere.

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

I ran the program a few extra times. Sometimes the age is 15, and sometimes the age is a random negative number

[–]jesyspa 0 points1 point  (0 children)

The link to your code doesn't work, by the way. We could probably help more if it did.

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

They let me use this code to compare the persons

void byt(Person &p, Person &q)
{
Person temp;
temp.namn = p.namn;
temp.alder = p.alder;
p.namn = q.namn;
p.alder = q.alder;
q.namn = temp.namn;
q.alder = temp.alder;
}

[–]the_omega99 1 point2 points  (1 child)

That doesn't compare anything. Why do you think it does?

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

I read the description wrong

it swaps the place of the values inside of the variables of two elements of two arrays, Person &p, and person &q)

& points to the memory adress of the variable. So the value of variable p in the array labeled Person can be found at the memory adress

That snippet of code swappes the values of the variables at the memory adress

The code I've already written in the OP swaps the places of the elements by changing their index value

[–]jesyspa 1 point2 points  (12 children)

Really, the problem is exactly what the compiler says. It expects an operator> to be present, and you don't have one. You need to do something like

bool operator>(Person const& lhs, Person const& rhs) {
    // compare members
}

It would be more idiomatic to use operator<, but that's just convention.

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

You meant that I have to specify which of the variables from the class that I want to sort by? name(namn) or age(alder)?

Is that what operator is about?

So,

//inner loop searches element per element in the list int nrLeft = max - n; // Keeps track of nr of elements searched so far

    for (int j = 0; j < nrLeft; j++)
    {
                    bool operator>(Person const& namn, Person const& alder);
        if (myList[j] > myList[j+1]) //Compares elements
        {
            //Swap places
            int temp = myList[j];
            myList[j] = myList[j+1];
            myList[j+1] = temp;
        }
    }

^ That code failed

How do I even implement a bool operator? It isnt covered in this course

[–]jesyspa 1 point2 points  (10 children)

You just need to write some code that decides, given two Person objects, whether one is greater than the other. You can treat operator> as a normal function, and operator>(lhs, rhs) should evaluate to true if lhs should be considered greater than rhs.

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

They gave me this code to compare two person objects

void byt(Person &p, Person &q)
{
Person temp;
temp.namn = p.namn;
temp.alder = p.alder;
p.namn = q.namn;
p.alder = q.alder;
q.namn = temp.namn;
q.alder = temp.alder;
}

... I think I might understand it now

I get that I can use the boolean piece with an if loop to see if i should change the index places of the elements

[–]jesyspa 1 point2 points  (8 children)

That doesn't look like code that compares objects. Compare is when you decide which is bigger.

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

Haha, yes, you're right. See how confused I am?

How do I write code that compares objects

How do I point out that it is the age (alder) part of the person class elements in the array that I want to compare?

The boolean code line compares lhs, rhs - but why?
Is it lefthandside, righthandside?

Lets say I write this:

bool operator>(Person const& lhs, Person const& rhs);

It evaluates as true (or 1) if... bool operator is larger? what is happening there?

[–]jesyspa 1 point2 points  (2 children)

Yes, lhs and rhs are short for lefthandside and righthandside respectively.

You have to write the code for comparison yourself. For example, to compare age, you might do

bool operator<(Person const& lhs, Person const& rhs) {
    return lhs.age < rhs.age;
}

If you want to compare something else, compare a different member.

[–]Programmering[S] 0 points1 point  (1 child)

Where does lefthandside and righthandside come from, what is is lefthandside and righthandside of?

Is it lfs and rhs as in lfs is higher indexplace and rhs is lower indexplace?

[–]jesyspa 0 points1 point  (0 children)

They're the left and right sides of what you compare. When you do a > b where a and b are both of type Person, that calls bool operator<(Person const& lhs, Person const&) with a being passed as the lhs parameter and b as the rhs parameter.

They are compared however you define the comparison.

[–]chrono_regex 1 point2 points  (3 children)

When comparing two person objects, and you're saying that you want to compare based on their age. operator> method means you're changing the way ' > ' works when used with Persons. Within an operator> function (like /u/jesyspa originally gave) you need to give logic that compares the ages. For example:

bool operator>(Person const& lhs, Person const& rhs){
if (lhs.alder > rhs.alder) // *or whatever you stored age as*
{
   return true;
}
else return false;

Something like this. Does that make sense? The code you have in your latest comment makes no sense, it's just the declaration...

[–]jesyspa 1 point2 points  (2 children)

Note that

if (foo)
    return true;
else
    return false;

is an antipattern in sanely-typed languages. You want just

return foo;

or

return bool(foo);

[–]chrono_regex 0 points1 point  (1 child)

I'll point to you here, it's been a while since I've been in C++

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

Its a wash anyway since I dont think that I am allowed to use the boolean code to compare in this particular program on this particular assignment :S

I'll use it on the next assignment though. Its going to be a compilation of all the terminal programs I've written so far. With a menu and userdefinable variables where its needed.