Hello, i'm posting here because:
1. I'm just a beginner and I'm not sure how some C++ codes work;
2. I'm trying to learn C++ from a Dummy book, I have no teacher/classmates to ask about these;
I was using #include<list> and "class" here. I wonder if .sort() only works for numbers/int because i've tried using it on strings but it didn't work. Or maybe I put it in the wrong place.
class Account
{
public:
Account(string sN, string sMN, string sLN, string sM, int nD, int nY)
...
Account* pA;
pA = new Account(getName(), getMidName(), getLastName(), getMonth(), getDate(), getYear());
if (pA)
{
accList.push_back(pA);
process(pA);
}
...
void displayResults(list<AccountPtr>& accntList)
{
accntList.sort();
for (list<AccountPtr>::iterator iter = accntList.begin();
iter != accntList.end();
iter++)
{
AccountPtr pAccount = *iter;
pAccount->display();
total += pAccount->dBalance();
}
}
I used .sort() before display but it didn't work. I also tried placing it at the getFunction() above but still it won't work. Should I place it inside the class or should I add some codes here?
The difference with your code is that you are trying to sort your own data type. Account
The standard function does not know how to compare 2 Accounts. So you have to tell it by writing a predicate function.
Account(string sN, string sMN, string sLN, string sM, int nD, int nY)
For example, i would consider sorting in ascending order with respect to sN.