I'm trying out custom operators for the first time. So far, just an assignment operator. The code seems to work properly until I try to use the new object, at which point it behaves oddly then crashes.
The class's code is fairly long, but the assignment operator (shown) is the only custom operator. There is no copy constructor.
The
class Points
is a linked list of
struct Point
.
class Points
also provides some basic statistical regression features which regress the points given by the linked list. It is intended to be part of a Statistics program.
The
class Points
can print itself out with a member function
void Print()
. When I make a new instance of the class,
aList->Print()
works. Inside of the assignment operator's definition,
this->Print()
works. But when I try
1 2
|
bList = aList;
bList->Print();
|
, it doesn't work.
I'm guessing that I need to work on my copy constructor somehow?
1 2 3 4 5 6 7 8 9 10 11 12 13
|
void main(){
Points *aList = OpenPointsFile("C:\\testfile.txt"), *bList;
//IMPORTANT:
aList->Print(); //works
bList = aList; //there's a this->Print() inside of this assignment operator that works
bList->Print(); //doesn't work
delete aList;
delete bList;
cin.ignore();
return;
}
|
1 2 3 4
|
struct Point{
Point *next;
double x, y;
}
|
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
class Points{
double slope, intercept, coefficient;
Point *first, *last;
long length;
bool recalc;
public:
Points(){length = 0;}
~Points(){
Point *current = first, *next;
while(current){
next = current->next;
delete current;
current = next;
}
length = 0;
}
Points *Points::operator=(const Points *inc){
Point *current = inc->first;
this->length = 0; //redundant due to constructor?
while(current){
cout << "Copying (" << current->x << "," << current->y << ")\n";
this->addPoint(current->x, current->y);
current = current->next;
}
this->slope = inc->slope;
this->intercept = inc->intercept;
this->recalc = inc->recalc;
this->coefficient = inc->coefficient;
this->Print(); //executes properly
return this;
}
void addPoint(double x, double y){
if (length){
last->next = new Point();
last = last->next;
} else {
first = new Point();
last = first;
}
last->x = x;
last->y = y;
last->next = NULL;
length++;
recalc = true;
}
void Print(){
Point *considered = first;
cout << "Points:\n";
while(considered){
cout << "(" << considered->x << "," << considered->y << ") at " considered << ";\n";
considered = considered->next;
}
if(this->getIntercept() < 0){
cout << "y = " << this->getSlope() << "x - " << abs(this->getIntercept()) << " (R^2 = " << this->getCoefficient() << ").\n";
} else {
cout << "y = " << this->getSlope() << "x + " << this->getIntercept() << " (R^2 = " << this->getCoefficient() << ").\n";
}
return;
}
}
|
PS- This code was typed out by hand; it's not a copy/paste. There may be bugs in it, and I didn't declare a lot of the variables and functions inside of the
class Points
. However, the real copy has worked through many trials for quite a while now. I think that I'm messing up the copy constructor somehow, or misunderstanding how they work.