assingment operator overloading. Error Unhandled exception at 0x103157aa (msvcr100d.dll) in 1exColo...

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 ...

Please help!
Thanks in advance
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
//lines.h
#ifndef LINES_H
#define LINES_H
#include "pointer.h"

#include <iostream>
using namespace std;

class Line
{
	 private:
      Point start; 
      string discrib;
	  Point end;

   public:
      //constructors and destructor
      Line();
      Line(string x, Point P_start, Point P_end);
      ~Line();
		
      //getters and setters
      void SetStart (const Point SomePoint);
      void SetEnd (const Point SomePoint);
      Point GetStart() const;
      Point GetEnd() const;

      //other useful methods
      void Print() const;
      void CalculateLineEquation(Point SomePoint);
      double CalculateDistance (const Point SomePoint) const;

	  Line operator = (const Line& l);
	  friend ostream& operator << (ostream& os , const Line& l);

  
};
#endif 


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
//pointer.h
#ifndef POINTER_H
#define POINTER_H

#include <iostream>

using namespace std; 


class Point

{
	 private:
	  double Xcoord;
	  double Ycoord;

   public:
      Point();
      Point(const Point &OtherPoint);
      ~Point();
      void Print()const;
      void SetXCoordinate(const double Value);
      void SetYCoordinate(const double Value);
      double GetXCoordinate() const;
      double GetYCoordinate() const;
      void CalculateDistance(const Point OtherPoint) const;

	  Point operator = (const Point& p);
	  friend ostream& operator << (ostream& os , const Point& p);

  

};

#endif  


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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//lines.cpp
#include <iostream>
#include <math.h>
#include "lines.h"
using namespace std;
#include <string>


Line::Line()
{
//	cout << "Privat, I'm line default constractor" << endl;
}
	
 

Line::Line(string x, Point P_start, Point P_end)
	:discrib(x), start(P_start), end(P_end)

{
 //  cout << "Gooten tak I'm line constructor" << endl;
}

 

Line::~Line()
{
	//cout << "salut I'm Line distructor. I'm killer " << endl; 
}

 

void Line::SetStart(const Point SomePoint)
{
   start = SomePoint;
}


void Line::SetEnd (const Point SomePoint)
{
  end = SomePoint;
}

 

Point Line::GetStart() const
{
   return start;
}

 

Point Line::GetEnd() const
{
   return end;
}

 

void Line::CalculateLineEquation(Point SomePoint) 
{
	 start = GetStart();
	 end = GetEnd();
	 return start.CalculateDistance(end);
}



void Line::Print() const
{
   cout << "Figure it out!" << endl;
   
}

Line Line::operator = (const Line& l)
	 {
	if (this == &l)
	{
		return * this; 
	}

	discrib = l.discrib;
	start = l.start;
	end = l.end;
	
	} 

ostream& operator << (ostream& os , const Line& l)
	{
		os << "[(" << l.discrib << "," << l.start << "," << l.end << ")]";
		return os ; 
	}


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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//Pointer.cpp
#include <iostream>
#include <math.h>
#include "pointer.h"

using namespace std;

 

Point::Point()
{
	//cout << "Hi, I'm default Point constructor" << endl; 
}

//Construct other points for calculating the points

Point::Point(const Point &OtherPoint)

{
	
//	cout << "\nHello, I'm Point constructor. " << endl;
	

   Xcoord = OtherPoint.Xcoord;
   Ycoord = OtherPoint.Ycoord;
}

 

//Destructor

Point::~Point()
{
	//cout << "\nHi, I'm Point distructor. " <<endl;
	//cout << "I'm already Kill PoDeCo and PoCo hahahahaha:))) "<< endl;
	
}

//Assign X-coordinate the Value that is put into the main

void Point::SetXCoordinate(const double Value)
{
    Xcoord = Value;
}

//Assing Y-coordinate the Value that is put into the main

void Point::SetYCoordinate(const double Value)

{
    Ycoord = Value;
}

//Return X-coordinate

double Point::GetXCoordinate() const

{
return Xcoord;
}

//Return Y-coordinate

double Point::GetYCoordinate() const

{
return Ycoord;
}

//Calculates distance between two points and outputs

void Point::CalculateDistance(const Point OtherPoint) const

{
double First = Xcoord;
double Second = OtherPoint.Xcoord;
double Third = Ycoord;
double Fourth = OtherPoint.Ycoord;
float Distance = sqrt((Second - First)*(Second-First) + (Fourth - Third)*(Fourth-Third));
cout << "The distance is: " << Distance << endl;
}


//Prints the points

void Point::Print() const
{
 cout << "Point is: (" << Xcoord << ", " << Ycoord << ")" << endl;
}


ostream& operator << (ostream& os , const Point& p)
	{
		os << "[" << p.GetXCoordinate() << "," << p.GetYCoordinate() << "]";
		return os ; 
	}



	 Point Point::operator = (const Point& p)
	 {
	if (this == &p)
		return * this; 

	Xcoord = p.Xcoord;
	Ycoord = p.Ycoord;
	} 
	
	  


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
//main.cpp
#include <iostream>
#include <math.h>
#include "pointer.h"
#include "lines.h"

using namespace 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");

}

Thanks in advance ones again
closed account (zb0S216C)
This raises issues:

1
2
3
4
5
6
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.

Also, what line does the compiler point to?

Wazzak
Last edited on
1> main.cpp
1> lines.cpp
1> Generating Code...

1>c:\all my\с++\level 3\ha level 3\exercise 5 line class\lines.cpp(82): warning C4715: 'Line::operator=' : not all control paths return a value

1>c:\all my\с++\level 3\ha level 3\exercise 5 line class\pointer.cpp(108): warning C4715: 'Point::operator=' : not all control paths return a value

1> 1exColonSynt_withColonSynt.vcxproj -> C:\all my\с++\HA level 5\Solution\3.4_SimpleInheritance\Debug\1exColonSynt_withColonSynt.exe
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

lines.cpp(82):

Line Line::operator = (const Line& l)
{
if (this == &l)
return * this;

discrib = l.discrib;
start = l.start;
line 81: end = l.end;
line 82: }

pointer.cpp(108):
Point Point::operator = (const Point& p)
{
if (this == &p)
return * this;

Xcoord = p.Xcoord;
Ycoord = p.Ycoord;
line107:
line108: }

How I should re write my code in this lines ?
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 ;
}

Thanks a lot !
Topic archived. No new replies allowed.