access violation error when returning value from operator overload

Nov 12, 2008 at 7:13am
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

// Operator Overloads
friend bool operator <(Student Student1, Student Student2); // Overload the < operator
friend bool operator >(Student Student1, Student Student2); // Overload the > operator

//


****************************************************************************
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
}

// ****************************************************************************


bool operator >(Student s1, Student s2) // Overload the > operator
{ int dummy = 99;
int dummy2 = 99;
dummy = s1.LastName.compare(0,s1.LastName.size(),s2.LastName);
dummy2 = s1.FirstName.compare(0,s1.FirstName.size(),s2.FirstName);
cout << endl << "dummy = " << dummy << " dummy2 = " << dummy2 << endl ;
cout << endl << "s1 is " << s1.LastName << " " << s1.FirstName << endl;
cout << endl << "s2 is " << s2.LastName << " " << s2.FirstName << endl;
if ( dummy == 0)
{ cout << endl << "dummy should = zero and it is = " << dummy << " and dummy2 = " << dummy2 << endl ;
if (dummy2>0)
{
cout << endl << "dummy should = zero it is = " << dummy << " and dummy2 should be greater then zero and it is = " << dummy2 << endl ;
return (true);
}
else {return (false);}
}
else {
if ( dummy > 0)
{ cout << endl << "dummy should greater then zero it does = " << dummy << " and dummy2 = " << dummy2 << endl ;
return (true);
}
else {return (false);}
}
}



Student *ptrS = new Student[NumberOfArrayElements]; // create an array to hold our data of the object type (class) Student



sort(ptrS);




void sort(Student A[])
{
int count= Student::GetStudentsEntered();
int i=0;
int j=0;
int dummy = 99 ;
int dummy2 = 99;
for (i = 0; i < count; i++)
{
for (j = i; (j > 0) ; j--)
{if (A[j] > A[j-1])
{
cout << endl << "i= " << i << " j= " << j << " j-1= " << j-1 << endl;
// swap(A[j], A[j-1]);
}
}
}
}


Last edited on Nov 12, 2008 at 7:14am
Nov 12, 2008 at 2:22pm
What does the Student class look like?

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.
Topic archived. No new replies allowed.