invalid conversion?

This is my program:
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <iostream>
using namespace std;

class Point
{
private: int x, y;
public:
   Point(int f = 0, int g = 0)
   {
      x = f;
      y = g;
   }
   int getX() const
   {
      return x;
   }
   int getY() const
   {
      return y;
   }
   void setX(const int new_x)
   {
      x = new_x;
   }
   void setY(const int new_y)
   {
      y = new_y;
   }
};
class PointArray
{
private:
   Point * loc;
   int len;
   public:
   PointArray()
   {
      len = 0;
      loc = new Point[0];
   }
   PointArray(const Point * points, const int size)
   {
      len = size;
      loc = new Point[len];
      for(int f = 0; f < len; f++)
         loc[f] = points[f];
   }
   PointArray(const PointArray& pv)
   {
      len = pv.len;
      loc = new Point[len];
      for(int f = 0; f < len; f++)
         loc[f] = pv.loc[f];
   }
   ~PointArray()
   {
      delete[] loc;
   }
   void resize(int n)
   {
      Point *loc1 = new Point[n];
      for(int f = 0; f < len && f < n; f++)
         loc1[f] = loc[f];
      len = n;
      delete[] loc;
      loc = loc1;
   }
   void pushBack(const Point &p)
   {
      resize(len+1);
      loc[len-1] = p;
   }
   void insert(const int pos, const Point &p)
   {
      resize(len+1);
      for(int f = len-1; f > pos; f--)
         loc[f] = loc[f-1];
      loc[pos] = p;
   }
   void remove(const int pos)
   {
      for(int f = pos; f < len-1; f++)
         loc[f] = loc[f+1];
      resize(len-1);
   }
   const int getSize() const
   {
      return len;
   }
   void clear()
   {
      resize(0);
   }
   Point * get(const int pos)
   {
      if (pos >= len)
         return NULL;
      else 
      {
         Point * x = new Point();
         *x = loc[pos];
         return x;
      }
   }
   const Point * get(const int pos) const
   {
      if (pos >= len)
         return NULL;
      else 
      {
         Point * x = new Point();
         *x = loc[pos];
         return x;
      }
   }
};
class Polygon
{
protected: 
   PointArray * loci; 
   int sides;
   static int N;
   public:
   Polygon(Point * loc, int len)
   {
      loci = new PointArray(loc, len);
      sides = len;
      N++;
   }
   Polygon(const PointArray& pv)
   {
      loci = new PointArray(pv);
      sides = pv.getSize();
      N++;
   }
   Polygon(const Polygon& pv)
   {
      loci = new PointArray(*pv.loci);
      sides = pv.sides;
      N++;
   } 
   ~Polygon()
   {
      delete loci;
      N--;
   }
   virtual double area() = 0;
   static int getNumPolygons()
   {
      return N;
   }
   int getNumSides()
   {
      return sides;
   }
   const PointArray * getPoints()
   {
      return loci;
   }
};
class Rectangle : public Polygon
{ 
private:
   typedef Polygon super;
   void makeRectangle(const Point e, const Point r)
   {
      Point * loci = new Point[4];
      loci[0] = e;
      loci[2] = r;
      loci[1] = new Point(e.getX(), r.getY());
      loci[3] = new Point(r.getX(), e.getY());
   }
   void makeRectangle (const int x1, const int x2, const int y1, const int y2)
   {
      Point * loci = new Point[4];
      loci[0] = new Point(x1, y1);
      loci[1] = new Point(x2, y1);
      loci[2] = new Point(x2, y2);
      loci[3] = new Point(x1, y2);
   }
};

The compiler is giving me these errors in the two overloaded makeRectangle() when they call the Point(int, int) constructor, saying:
geometry.cpp: In member function 'void Rectangle::makeRectangle(Point, Point)':
geometry.cpp:170:45: error: invalid conversion from 'Point*' to 'int' [-fpermissive]
geometry.cpp:8:4: error: initializing argument 1 of 'Point::Point(int, int)' [-fpermissive]
geometry.cpp:171:45: error: invalid conversion from 'Point*' to 'int' [-fpermissive]
geometry.cpp:8:4: error: initializing argument 1 of 'Point::Point(int, int)' [-fpermissive]
geometry.cpp: In member function 'void Rectangle::makeRectangle(int, int, int, int)':
geometry.cpp:176:33: error: invalid conversion from 'Point*' to 'int' [-fpermissive]
geometry.cpp:8:4: error: initializing argument 1 of 'Point::Point(int, int)' [-fpermissive]
geometry.cpp:177:33: error: invalid conversion from 'Point*' to 'int' [-fpermissive]
geometry.cpp:8:4: error: initializing argument 1 of 'Point::Point(int, int)' [-fpermissive]
geometry.cpp:178:33: error: invalid conversion from 'Point*' to 'int' [-fpermissive]
geometry.cpp:8:4: error: initializing argument 1 of 'Point::Point(int, int)' [-fpermissive]
geometry.cpp:179:33: error: invalid conversion from 'Point*' to 'int' [-fpermissive]
geometry.cpp:8:4: error: initializing argument 1 of 'Point::Point(int, int)' [-fpermissive]
Because x1, x2, y1, and y2 are integers and therefore should be compatible with the Point(int, int) constructor, I do not understand why it is giving me the error: "invalid conversion from 'Point*' to 'int'".
You can't save pointers to an array of non-pointer objects. The operator "new" dynamically allocates the object and returns a pointer to it not the instance of the object itself. What are you trying to do on Line 167? Is it meant to be a pointer to an array or a pointer to an array of pointers? Either one is valid.
your errors seem to refer to the Point constructor. However, the problem is that you are trying to assign a pointer to an object, as clang points out:
foo.cpp:170:12: error: no viable overloaded '='
                        loci[1] = new Point(e.getX(), r.getY());
                        ~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
foo.cpp:4:7: note: candidate function (the implicit copy assignment operator) not viable: no known conversion from 'Point *' to 'const Point' for 1st argument; dereference the argument with *


the solution is not what clang proposes, but simply dropping the new
loci[1] = Point(e.getX(), r.getY());

By the way, you are leaking memory as `loci' is a local variable in `makeRectangle()', you are not referring to the member `loci'

Your `PointArray' lacks an adequate assignment operator. You should simple use std::vector
1
2
3
4
class Polygon
{
protected: 
   PointArray * loci; //¿is there a good reason to use a pointer here? 
Omitting the 'new' before each of the calls to the Point(int, int) constructor resolved the error. The compiler's error message was not very clear to me because I did not see where I was trying to convert from 'Point*' to 'int'.

Thank you for helping.
Topic archived. No new replies allowed.