【发布时间】:2013-08-20 19:06:18
【问题描述】:
我想定义从 QVector 中删除自定义类型对象和索引的函数。 原文原文如下:
Point PointCollection::RemovePoint(int index)
{
Point removedPoint = new Point(this[index].Id, this[index].X, this[index].Y);
this->remove(index);
updateCentroid();
return (removedPoint);
}
Point PointCollection::RemovePoint(Point p)
{
Point removedPoint = new Point(p.GetId(), p.GetX(), p.GetY());
this.remove(p);
updateCentroid();
return (removedPoint);
}
由于new,它没有像我想的那样工作。然后我将源代码修改为:
Point PointCollection::deletePoint(int Index)
{
Point deleted = Point(this[Index].Id, this[Index].X, this[Index].Y);
this->remove(Index);
updateCentroid();
return(deleted);
}
Point PointCollection::deletePoint(Point point)
{
Point deleted = Point(point.GetId(), point.GetX(), point.GetY());
this->remove(point);
updateCentroid();
return(deleted);
}
现在Point PointCollection::deletePoint(int Index) 编译没有任何错误,但Point PointCollection::deletePoint(Point point) functioned 中的this->remove(point); 编译时出现以下错误:
错误:没有匹配函数调用 'PointCollection::remove(Point&)'
Q1:我是否正确删除了new?
Q2:如何解决我遇到的错误。
【问题讨论】:
-
Point removedPoint = new Point?不应该是指针吗?你的代码有很多问题。什么是“自定义类型对象”? -
你不能说
this[someIndex](好吧,你可以,但会导致灾难)。 -
你有没有试过这个=0 :)
-
@ddriver "自定义类型对象" - 我的意思是类型对象是Point,类型Point是我定义的。 |你的意思是 Point *removedPoint = new Point ?
-
@juanchopanza 仍然可以正常编译。为什么会导致灾难?什么是替代品?