i am trying to brush off old skills. i use to write c and i am trying to learn c++. i have a program that is getting an access violation after several passes through my > operator overload. The > operator overload is accessed in the sort routine. I have commented out the swap for not and i am simply returning a true / false value from the operator overload. I can not find where i am misusing memory or pointers. i put a number of trace lines in to localize the problem and still can not figure it out. Does anyone have any ideas?
i have attached part of the source code here. i am using Dev-C++ 4.9.9.2
****************************************************************************
bool operator <(const Student s1, const Student s2) // Overload the < operator
{
if (( s1.LastName.compare(0,s1.LastName.size(),s2.LastName) < 0) && (s1.FirstName.compare(0,s1.FirstName.size(),s2.FirstName)<0))
{return(true); }
else {return (false); } // returns a indicating of whether the s1 name is logically greater then the s2 name
}
Your operators should be taking Students by const reference, not by value. As your code is written, every time operator< or operator> are called you are copying two Students (ie, running the copy constructor). If your Student class has pointers in it and you didn't write your own copy constructor, then you'll need to write a copy constructor.