**已关闭。**此问题需要debugging details。当前不接受答案。
编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
3天前关闭。
这篇文章是编辑和提交审查3天前。
Improve this question
下面的代码定义:
- Vertex类,包含一个id和3个坐标(x、y和z)作为私有成员变量
- Triangle类,包含一个id和3个Vertexes作为私有成员变量
/*
-------------------------------------------------------
Class definition of the Vertex class
-------------------------------------------------------
*/
class Vertex
{
private:
int id;
float coordinates[3];
public:
// Constructor
Vertex(int newId, float *newCoordinates)
: id(newId)
{
// copies coordinates across
setCoordinates(newCoordinates);
}
// Copy Constructor
Vertex(const Vertex &copiedVertex)
: id(copiedVertex.getId())
{
// copies coordinates across
setCoordinates(copiedVertex.coordinates);
}
// Destructor
virtual ~Vertex() {}
// Assignment Operator
const Vertex &operator=(const Vertex &assignedVertex)
{
// if assigned itself, return reference to itself
if (this == &assignedVertex)
return (*this);
setId(assignedVertex.getId());
setCoordinates(assignedVertex.coordinates);
// return reference to itself
return (*this);
}
// --- Gets data from the object and returns it ---
int getId() const { return id; }
float getX() const { return coordinates[0]; }
float getY() const { return coordinates[1]; }
float getZ() const { return coordinates[2]; }
// --- Sets data inside the object ---
bool setId(int newId)
{
id = newId;
return true;
}
bool setX(float newX)
{
coordinates[0] = newX;
return true;
}
bool setY(float newY)
{
coordinates[1] = newY;
return true;
}
bool setZ(float newZ)
{
coordinates[2] = newZ;
return true;
}
bool setCoordinates(const float *newCoordinates)
{
// copies coordinates across
for (int i = 1; i <= 3; i++)
coordinates[i] = newCoordinates[i];
return true;
}
};
/*
-------------------------------------------------------
Class definition of the Triangle class
-------------------------------------------------------
*/
class Triangle
{
private:
int id;
Vertex *vertexes[3];
bool clearVertexes()
{
for (int i = 0; i < 3; i++)
delete vertexes[i];
return true;
}
public:
// Constructor
Triangle(int newId, Vertex **newVertexes)
: id(newId)
{
setVertexArray(newVertexes);
}
// Copy Constructor
Triangle(Triangle &copiedTriangle) : id(copiedTriangle.id)
{
setVertexArray(copiedTriangle.getVertexArray());
}
// Destructor
virtual ~Triangle() { clearVertexes(); }
// Assignment Operator
const Triangle &operator=(const Triangle &assignedTriangle)
{
// if assigned itself, return reference to itself
if (this == &assignedTriangle)
return (*this);
setId(assignedTriangle.getId());
setVertexArray(assignedTriangle.getVertexArray());
// return reference to itself
return (*this);
}
// --- Gets data from the object and returns it ---
int getId() const { return id; }
Vertex *getPointerToVertex(int index) const { return vertexes[index]; }
Vertex **getVertexArray() const { return vertexes; }
// --- Sets data inside the object ---
bool setId(float newId)
{
id = newId;
return true;
}
bool setVertexPointer(int index, Vertex *newPtr)
{
vertexes[index] = newPtr;
return true;
}
bool setVertexArray(Vertex **newArray)
{
for (int i = 0; i < 3; i++)
setVertexPointer(i, newArray[i]);
return true;
}
};
我尝试在Triangle
复制构造函数和赋值运算符中使用Triangle::getVertexArray()
。
当编译(g++)时,我得到了以下错误:
In member function 'Vertex** Triangle::getVertexArray() const':
error: invalid conversion from 'Vertex* const*' to 'Vertex**'
有没有人能解释一下这个问题的来源,以及如何在我的背景下解决它?
1条答案
按热度按时间t98cgbkg1#
getVertexArray
是一个const
方法,因此对象中的所有字段都是const
,这意味着在此上下文中,vertexes
数组是const
,指向它的指针是Vertex * const *
要修复它,您需要从方法中移除
const
或将其添加到返回类型中。