为什么当我给出的参数是int时,这是一个无效的转换
why is this an invalid conversion when the argument I am giving is an int
这是我的程序:
#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);
}
};
编译器在两个重载的makeRectangle()中给我这些错误,当它们调用Point(int, int)构造函数时,说:
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]
因为x1, x2, y1和y2是整数,因此应该与Point(int, int)构造函数兼容,我不明白为什么它会给我错误:"从'Point*'到'int'的无效转换"。
getter和setter
为了使用getter和setter而使用它们绝不是一个好主意。因此,Point
的代码可以简化为:
struct Point {
int x;
int y;
};
这不仅更容易、更快地阅读,而且与您的解决方案一样始终正确。
数组
您的PointArray
类是不可重用的。您可以使用标准容器。您的PointArray
类可以减少到:
using PointArray = std::vector<Point>;
Polygon基类
Polygon
类也可以得到改善。首先,您不需要动态分配loci
。你应该使用:
class Polygon {
protected:
PointArray loci;
int sides;
static int N;
现在,对于构造函数,您可以使用以下内容来代替您期望从第一个构造函数中获得的c风格数组:
public:
Polygon(const PointArray& loc) {
loci = loc
sides = loc.size();
N++;
}
如您所见,不需要len
参数。您还可以使用std::initializer_list
来允许如下表达式:
Polygon x { Point(...), Point(...), Point(...), ... };
方法如下:
Polygon(const std::initializer_list<Point> list)
: loci(list)
, sides(list.size())
{ N++; }
在你的智能编辑之后,你可以摆脱自定义复制构造函数和析构函数,有效地遵循零规则。
其余部分基本相同:
virtual double area() = 0;
static int getNumPolygons() { return N; }
int getNumSides() { return sides; }
除了getPoints
。我建议两种不同的重载:const
和非const
:
PointArray& getPoints() { return loci; }
const PointArray& getPoints() const { return loci; }
};
矩形类
最后你的矩形类当然可以改进:
class Rectangle : public Polygon {
private:
void makeRectangle(const Point& e, const Point& r) {
loci.push_back(e);
loci.push_back(r);
loci.emplace_back(e.x, r.y);
loci.emplace_back(r.x, e.y);
}
void makeRectangle (const int x1, const int x2, const int y1, const int y2) {
loci.emplace_back(x1, y1);
loci.emplace_back(x2, y1);
loci.emplace_back(x2, y2);
loci.emplace_back(x1, y2);
}
};
结论
在这个简单的重构之后,你所有的错误都应该消失了,你的代码应该更短,可读性更强。
相关文章:
- 错误:从"int"到枚举c++的转换无效
- 从 'int' 到 'int*' CPP 的转换无效
- C++ PTHREADS - 无效转换无效*(*)()到无效*(*)(无效*)
- 为什么我收到"从常量指针到指针的转换无效?
- 错误:从"int"到"int*"的转换无效[-允许].我在下面提供了我的代码,我
- 从类指针到 int 的转换无效
- C++ 错误:由于 swprintf 而从 'int' 到 'const wchar_t* 的转换无效
- 错误:从'void*'到'const uint8_t* {aka const unsigned char*}'的转换无效 [-允许]
- QTextStream的操作数到二进制的转换无效
- 从类型"void*"到函数指针的强制转换无效
- 错误:从 'char' 到 'const char' [-允许] strcat(加密,密钥[i])的转换无效;
- C++ - 从 "int" 到 "unsigned char" 的缩小转换无效
- 返回对象时从'const DList<int>* const'到 'DList<int>*' [-fallowive] 的转换无效
- 错误:AES 加密密钥:从"char*"到"无符号字符"的转换无效
- 模板类实例化中的指针转换无效
- 从"int"到"矢量*"的转换无效
- 从类型"void*"到类member_function指针的强制转换无效
- 在 sprintf 中从 'signed char*' 到 'char*' 的转换无效
- 另一个:从"常量类型*"到"类型*"的转换无效
- constexpr 链表 - 从 const X* 到 X* 的转换无效