联合shared_ptr的向量

时间:2011-11-03 14:33:24

标签: c++ tr1

我通过以下方式使用智能指针:

typedef std::tr1::shared_ptr<RealAlgebraicNumber> RealAlgebraicNumberPtr;

我正在使用智能指针,因为RealAlgebraicNumber是一个“抽象”超类。它工作正常,现在我想要一个类来封装一个向量。它看起来像这样:

class RealAlgebraicPoint
{
public:
    RealAlgebraicPoint (vector<RealAlgebraicNumberPtr>& v);
    RealAlgebraicPoint (RealAlgebraicPoint& R);
    RealAlgebraicPoint conjoin (const RealAlgebraicNumber& N);
private:
    vector<RealAlgebraicNumberPtr> mNumbers;
    unsigned int mSize;
};

实施是这样的:

RealAlgebraicPoint::RealAlgebraicPoint(vector<RealAlgebraicNumberPtr>& v)
    : mNumbers(v), mSize(mNumbers.size()) {}
RealAlgebraicPoint::RealAlgebraicPoint(RealAlgebraicPoint& R)
    : mNumbers(R.mNumbers), mSize(mNumbers.size()) {}
RealAlgebraicPoint RealAlgebraicPoint::conjoin (const RealAlgebraicNumber& N)
{
    vector<RealAlgebraicNumberPtr> v;
    // do something fancy with v!
    return RealAlgebraicPoint(v); // this is line 58
}

不幸的是,我得到了这样的丑陋错误:

RealAlgebraicPoint.cpp: In member function 'GiNaC::RealAlgebraicPoint GiNaC::RealAlgebraicPoint::conjoin(const GiNaC::RealAlgebraicNumber&)':
RealAlgebraicPoint.cpp:58:32: error: no matching function for call to 'GiNaC::RealAlgebraicPoint::RealAlgebraicPoint(GiNaC::RealAlgebraicPoint)'
RealAlgebraicPoint.cpp:46:1: note: candidates are: GiNaC::RealAlgebraicPoint::RealAlgebraicPoint(GiNaC::RealAlgebraicPoint&)
RealAlgebraicPoint.cpp:42:1: note: GiNaC::RealAlgebraicPoint::RealAlgebraicPoint(std::vector<std::tr1::shared_ptr<GiNaC::RealAlgebraicNumber> >&)
RealAlgebraicPoint.cpp:38:1: note: GiNaC::RealAlgebraicPoint::RealAlgebraicPoint(unsigned int&)

有什么想法吗?我真的很沮丧为什么他打电话给RealAlgebraicPoint(GiNaC::RealAlgebraicPoint),因为我只打电话给RealAlgebraicPoint(std::vector<std::tr1::shared_ptr<GiNaC::RealAlgebraicNumber> >&)

谢谢!

勒夫

1 个答案:

答案 0 :(得分:3)

复制构造函数应该引用const。