User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 363,808 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,630 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:

C++ Bubble sort not working with array of records, does work with simple array.

Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 9,916
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 32
Solved Threads: 793
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Posting Prodigy

Re: C++ Bubble sort not working with array of records, does work with simple array.

  #4  
May 9th, 2008
What I'm doing is writing my own version of the = operator. So when you way temp = student[j]; the compiler will call my version of the = operator instead of the compiler's default version. The parameter to the operator is the equilivent of passing student[j] by reference to any normal function.

I believe the reason your sort does not work as you wrote it is because the structure contains c++ STL class -- std::string. In such cases when the structure includes other class objects you should write the overloaded = operator.

That might also be similar to writing a swap function
void swap(if1& f1, if1& f2)
{
    if1 temp;
    temp.idnum = f1.idnum;
    temp.lastname = f1.lastname;
    temp.firstname = f1.firstname;
    temp.examscore = f1.examscore;
   temp.hwscore = f1.hwscore;

   f1.idnum = f2.idnum;
   // etc for f1

   f2.idnum = temp.idnum;
   // etc for f2
}

As you can see, the above code is pretty lengthly. The overloaded = operator is consideratly shorter and less pron to typing errors.
Last edited by Ancient Dragon : May 9th, 2008 at 7:56 pm.
'Politics' is made up of two words, 'poli,' which is Greek for 'many,' and 'tics,' which are blood-sucking insects.
- Gore Vidal
Being ignorant is not so much a shame as being unwilling to learn. - Benjamin Franklin
Reply With Quote  
All times are GMT -4. The time now is 11:31 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC