C++ compilation issue on class inheritance

As I am fairly new in inheritance, I have some compilation error which I do not know how to resolve. I have no where to go to and hence I come here. I would very much appreciate it if someone could help me. Thanks!

Compilation error:
main.cpp:58:35: error: ‘Point& operator=(const Point&)’ must be a nonstatic member function
Point& operator = (const Point &a)
^
main.cpp: In function ‘int main()’:
main.cpp:95:7: error: no matching function for call to ‘Line::Line()’
Line l1; //Output: Line construction
^~

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
  class Point
	{
    private:
        int x, y;
    public:
        Point() : x(0), y(0) {}
        Point(int x, int y) : x(x), y(y) {}
		friend ostream &operator<<(ostream &stream, Point &p);
		Point& operator = (const Point &a);		
	};

class Line : public Point 
	{ 
	 private:
        Point p1;
        Point p2;
    public:
        Lin(const Point & p1, const Point & p2 ) : p1(p1), p2(p2) {}

		void Draw();
		
	};
	
	ostream &operator<<(ostream &stream, Point &p) {
		stream << "(" << p.x << ", " << p.y << ")" << endl;
		return stream;
	}
	
	Point& operator = (const Point &a)
	{
		x = a.x;
		y = a.y;
		return *this;
	}
	void Line::Draw() {
    cout << "Line" << "drawing" << endl;
	}
int main() //Cannot change
{
	Lin l1; 				//Output: Line construction
	std::cout << l1; 		//Output: Line (0,0) (0,0)
	l1.Drw(); 				//Output: Line drawing
	
	Line 2(Point(), 
	Poin(100,100));	//Output: Line construction
	std::cout << l2; 		//Output: Line (0,0) (100,100)
	l1 = l2;
	std::cout << l1; 		//Output: Line (0,0) (100,100)
}
Last edited on
closed account (E0p9LyTq)
You need a default Line class constructor that takes no parameters:
Line() : p1(Point(0, 0)), p2(Point(0, 0)) {}

It is Point& Point::operator= (const Point& a)

You also need to overload std::ostream's operator<< for your Line class, as well as overload operator= for Line.
1
2
3
4
5
6
7
8
9
10
11
12
std::ostream& operator<<(std::ostream& stream, Line& line)
{
   std::cout << line.p1 << ", " << line.p2 << '\n';
   return stream;
}

Line& Line::operator= (const Line& a)
{
   p1 = a.p1;
   p2 = a.p2;
   return *this;
}

Eliminating the end-line in Point's operator<< overload the output is:
Line: (0, 0), (0, 0)
Line drawing
Line: (0, 0), (100, 100)
Line: (0, 0), (100, 100)

So you can see the changes needed in context:
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
#include <iostream>

class Point
{
private:
   int x;
   int y;

public:
   Point() : x(0), y(0) {}
   Point(int x, int y) : x(x), y(y) {}

public:
   friend std::ostream& operator<<(std::ostream& stream, Point& p);
   Point& operator= (const Point &a);
};

std::ostream &operator<<(std::ostream &stream, Point &p)
{
   stream << "(" << p.x << ", " << p.y << ")";
   return stream;
}

Point& Point::operator= (const Point& a)
{
   x = a.x;
   y = a.y;
   return *this;
}

class Line : public Point
{
private:
   Point p1;
   Point p2;

public:
   Line() : p1(Point(0, 0)), p2(Point(0, 0)) {}
   Line(const Point & p1, const Point & p2) : p1(p1), p2(p2) {}

public:
   void Draw();
   friend std::ostream& operator<<(std::ostream& stream, Line& line);
   Line& operator= (const Line&);
};

void Line::Draw()
{
   std::cout << "Line drawing\n";
}

std::ostream& operator<<(std::ostream& stream, Line& line)
{
   std::cout << "Line: " << line.p1 << ", " << line.p2 << '\n';
   return stream;
}

Line& Line::operator= (const Line& a)
{
   p1 = a.p1;
   p2 = a.p2;
   return *this;
}

int main() //Cannot change
{
   Line l1; 				// no output

   std::cout << l1; 		//Output: Line (0,0) (0,0)

   l1.Draw(); 				//Output: Line drawing

   Line l2(Point(),
           Point(100, 100));	// no output

   std::cout << l2; 		//Output: Line (0,0) (100,100)

   l1 = l2;
   std::cout << l1; 		//Output: Line (0,0) (100,100)
}
Last edited on
But why would line inherit from Point?
It just needs to contain two points, as it does.

And don't leave out your includes when you post code, @playpro! :-(
Last edited on
> Point& operator = (const Point &a)
This doesn't seem to be inside your class.

> error: no matching function for call to ‘Line::Line()’
You need a default Line constructor.
 
Line(){}
Topic archived. No new replies allowed.