我使用的代码结构存在问题,如下(简化):
class SPoint
{
public:
SPoint(double x, double y, double z) : _x(x), _y(y), _z(z) {}
protected:
double _x, _y, _z;
}
class Point3D : public SPoint
{
public:
Point3D(double x, double y, double z) : SPoint(x, y, z) { // default values for U and V }
protected:
double U, V;
}
这些点用于创建折线:
class SPolyline
{
public:
SPolyline(const vector<shared_ptr<SPoint>>& points) { // points are cloned into _points}
protected:
vector<shared_ptr<SPoint>> _points;
};
class Polyline3D : SPolyline
{
public :
Polyline3D(const vector<shared_ptr<Point3D>>& points) : SPolyline(points) // doesn't compile
};
当我尝试使用此错误编译 Polyline3D 时,VS2010 拒绝我
error C2664: 'SPolyline::SPolyline(const std::vector<_Ty> &)' : cannot convert parameter 1 from 'const std::vector<_Ty>' to 'const std::vector<_Ty> &'
with
[
_Ty=std::tr1::shared_ptr<SPoint>
]
and
[
_Ty=std::tr1::shared_ptr<Point3D>
]
and
[
_Ty=std::tr1::shared_ptr<SPoint>
]
Reason: cannot convert from 'const std::vector<_Ty>' to 'const std::vector<_Ty>'
with
[
_Ty=std::tr1::shared_ptr<Point3D>
]
and
[
_Ty=std::tr1::shared_ptr<SPoint>
]
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
没有从vector<shared_ptr<Derived>>
到的默认转换vector<shared_ptr<Base>>
。知道我需要对折线中的点进行共享所有权,如何解决这个问题?我使用的shared_ptr
是标准的,不是来自 Boost 的。