gpt4 book ai didi

c++ - 连接 shared_ptr 的 vector

转载 作者:行者123 更新时间:2023-11-30 03:05:09 26 4
gpt4 key购买 nike

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

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。

关于c++ - 连接 shared_ptr 的 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7996653/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com