There two Lines (class Line) each contain two points(class Point) and description (private data of class Line). I what to assign coordinate one Line to another, but visual studia gave me some evil mistake like Error Unhandled exception at 0x103157aa (msvcr100d.dll) in 1exColonSynt_withColonSynt.exe: 0xC0000005: Access violation reading ...
//main.cpp
#include <iostream>
#include <math.h>
#include "pointer.h"
#include "lines.h"
usingnamespace std;
int main()
{
// Define two objects within Point class
Point p1;
Point p2;
//randomize four points
//Set these points into different x and y values=
p1.SetXCoordinate(14);
p1.SetYCoordinate(15);
p2.SetXCoordinate(16);
p2.SetYCoordinate(17);
Line MyLine1("Pipec",p1,p2 );
Point p33;
Point p43;
p33.SetXCoordinate(24);
p33.SetYCoordinate(25);
p43.SetXCoordinate(21);
p43.SetYCoordinate(22);
//p33 = p2;
Line test1 ("hi",p33,p43 );
test1 = MyLine1;
cout << MyLine1<<endl;
cout << test1 ;
cout << endl;
system ("pause");
}
Line Line::operator = (const Line& l)
{
if (this == &l)
{
return * this;
}
Here, you're retuning a new Line object which is copy-initialised with the current instance. Normally, you'd return a reference to the current class instance; in this case, a Line instance.
Thanks I realized what was happening
Should be
Line Line::operator = (const Line& l)
{
if (this == &l)
{
return * this;
}
discrib = l.discrib;
start = l.start;
end = l.end;
return *this ;
}