using sort() on protected variables

Hi! I was using #include <list> and sort it with sort(). The problem is that i cant sort the member variables outside the class because they were set to protected, I can only call the access functions but then this wouldn't change the original order of the variables, when I tried writing the bool operator<(Account& s1, Account& s2) inside the class the compiler complains that it must take exactly one argument. I want the member variable to stay protected and then sort it from inside the class. I wish you could help me with this.
We need to see your code to tell you exactly what's wrong.

However, your bool operator< should only have one argument, not two.
Sorry, here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Account
{
public:
      Account(string sName, string sMName, string sLName)
     {
          name = sName;
          midName = sMName;
          lastName = sLName;
      }

// proto

void display;

// access functions

string iName() {return name;}
string iMName() {return midName;}
string iLName() {return lastname;}

protected:
      string name;
      string midName;
      string lastName;
};

bool operator<(const Account& s1, const Account& s2)
{
   return s1.lastName < s2.lastName; // won't work because it's protected
}

void Account::display()
{}

typedef Account* AccPtr;

int main()
{
list<AccPtr> listAcc;

getAcc(listAcc);

listAcc.sort();

display(listAcc);
}


the next is here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Account
{
public:
      Account(string sName, string sMName, string sLName)
     {
          name = sName;
          midName = sMName;
          lastName = sLName;
      }

// access functions

string iName() {return name;}
string iMName() {return midName;}
string iLName() {return lastname;}

// proto

void display();

bool operator<(const Account& s1, const Account& s2) // won't work
{
   return s1.lastName < s2.lastName;
}

protected:
      string name;
      string midName;
      string lastName;
};

void Account::display()
{}

typedef Account* AccPtr;

int main()
{
list<AccPtr> listAcc;

getAcc(listAcc);

listAcc.sort();

display(listAcc);
}
Not sure what you're trying to point out with the two copies of your class.

Several problems:
1) Your listAcc is a list<AccPtr> i.e. a list of pointers, so sort will be expecting a < operator that accepts a const AccPtr. If you want to sort a list of pointers based on name ordering, you will have to dereference the pointers.

2) The < operator should be public. You have it as protected. list<>.sort can't call a protected member.

3) As Pindrought pointed out, the < operator should take ONE argument.

This has nothing to do with the fact that the variables are protected. As long as the < operator is a member of the class it will see the protected members without issue.

BTW, you're only sorting on last name. No guarantee that if two last names are the same, the entries will also be in order by first name.



Topic archived. No new replies allowed.