我通过以下方式使用智能指针:
typedef std::tr1::shared_ptr<RealAlgebraicNumber> RealAlgebraicNumberPtr;
我使用智能指针的原因是因为RealAlgebraicNumber是“抽象”父类(super class)。它工作正常,现在我要一个类来封装 vector 。看起来像这样:
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> >&)
。谢谢!
约阿希姆
最佳答案
复制构造函数应引用const。