我想定义删除自定义类型对象和QVector索引的函数。 最初来源如下:指针和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)
编译没有任何错误,但this->remove(point);
在Point PointCollection::deletePoint(Point point)
运作与以下错误编译:
error: no matching function for call to 'PointCollection::remove(Point&)'
Q1:我做的是去除new?
正确Q2:如何解决我遇到的错误。
'Point removedPoint = new Point'?这不应该是一个指针吗?很多东西与您的代码错误。什么是“自定义类型对象”? – dtech
你不能说'这[someIndex]'(可以,但它会导致灾难)。 – juanchopanza
你有没有试过这个= 0 :) – user2672165