C++ compilation issue

Hi I am trying to figure out why am I getting some compilation issue with my codes as below? After I tried incorporating class polygon into my line class it gave me a compilation error.

Compilation error:

main.cpp: In constructor ‘Line::Line()’:
main.cpp:55:28: error: no matching function for call to ‘Polygon::Polygon(int, int)’
Line() : p1(Polygon(0, 0)), p2(Polygon(0, 0)) {}
^
main.cpp:41:8: note: candidate: Polygon::Polygon()
class Polygon : public Shape
^~~~~~~

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
class Point
{
private:
   int x;
   int y;

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

public:
  ...
};
class Shape 
{ 
public:
 virtual void  Draw();
};
	
class Polygon : public Shape 
{ 
public:
 Point points; 
};

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

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

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

void Line::Draw()
{
   std::cout << "Line drawing\n";
}
Last edited on
(Polygon(0, 0))

This is an attempt to use a particular constructor for the class Polygon.

The constructor you're trying to use is the one that accepts two int values.

Let's take a look at the constructors you have written for your Polygon class:
1
2
3
4
5
class Polygon : public Shape 
{ 
public:
 Point points; 
};

You didn't write any. You're trying to use a constructor that doesn't exist. You can't do this. You can only use constructors that exist.

Right. Ok. Thanks repeater. So then how do I include my class point into my Polygon class? Not sure about how to do it... Or do I omit my class point and use class polygon instead. In short is class Point needed then?

Last edited on
I see that your class Point contains constructors you wrote:
1
2
3
4
5
6
7
8
9
10
11
12
13
class Point
{
private:
   int x;
   int y;

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

public:
  ...
};

You just need to write a suitable constructor in Polygon.

In short is class Point needed then?

It's your code; you tell me. In the code above, your Polygon class contains a single Point object. Which you have named points; odd name, given that it is just one single Point.
Last edited on
1
2
3
4
5
class Line : public Polygon
{
private:
   Polygon p1;
   Polygon p2;

Hmm, a Line is a Polygon that has two additional Polygons as members. Why?


Lets assume that Polygon has constructor Polygon::Polygon( std::string ).
Now you write a constructor for Line:
1
2
3
4
5
6
7
Line()
: Polygon( "Huey" ), // initialize the base class
  // initialize the members
  p1( "Dewey" ), 
  p2( "Louie" )
{
}
Topic archived. No new replies allowed.