C ++-vector <>中的std :: unique_ptr为nullptr

T0T0R

我想将Particle对象存储在一个vector对象中,以便以后可以访问它。这些粒子(ElectronsProtons)从Particle包含toString()虚拟方法继承toString()然后在ElectronProton类中重写方法

读取向量容器时,我想访问toString()特定于ElectronProton而不是的方法Particle

显然,一种方法是使用std::unique_ptr这是我尝试运行的代码部分:

int main(){
    /**/
    std::vector<std::unique_ptr<Particle>> particles(nbParticles);

    particles.push_back(std::unique_ptr<Electron>( new Electron(1.0, 2.0, 3.0)));
    particles.push_back(std::unique_ptr<Proton>(new Proton(1.0, 2.0, 3.0)));
    particles.push_back(std::unique_ptr<Particle>(new Particle(0.0, 0.0, 1.0, 2.0, 3.0)));

    if (particles[0]==nullptr){
        std::cout<< "index=0 : nullptr"<<std::endl; //There is a null_ptr at particles[0]
    }

    if (particles[2]==nullptr){
        std::cout<< "index=2 : nullptr"<<std::endl; //There is not a null_ptr at particles[2]
    }

    std::cout<<particles[0]->toString()<<std::endl; //This is what I'm trying to do
    /**/
}

指向Particle对象的指针似乎不错,但不能指向ElectronProton我猜构造函数有问题吗?

class Particle
{
public:
    Particle();
    Particle(double mass, double charge, double posX, double posY, double posZ);
    virtual std::string toString() const;
}

class Electron : public Particle
{
public:
    Electron(double PosX, double PosY, double PosZ);
    virtual std::string toString() const;
}

class Proton : public Particle
{
public:
    Proton(double PosX, double PosY, double PosZ);
    virtual std::string toString() const;
}

以及定义:

Particle::Particle(double mass, double charge, double posX, double posY, double posZ) :
    m_mass(mass), m_charge(charge),
    m_posX(posX), m_posY(posY), m_posZ(posZ) {}


Electron::Electron(double PosX, double PosY, double PosZ) :
    Particle(9.109E-31, -1.602E-19, PosX, PosY, PosZ){}

Proton::Proton(double PosX, double PosY, double PosZ) :
    Particle(9.109E-31, +1.602E-19, PosX, PosY, PosZ){}
布赖恩

您犯了一个经典错误,即使是最有经验的C ++程序员也会犯错:您以初始大小声明了向量,然后push_back为其添加了其他元素,而不是分配给现有元素。通过(nbParticles)从向量初始化中删除解决此问题

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将std :: unique_ptr推回std :: vector时,编译器不会失败

调整大小std :: vector <std :: unique_ptr <T >>的性能

C ++中的std :: vector与std :: array

C ++ Qt std :: unique_ptr Qt版本在哪里?

声明包含std :: unique_ptr的结构的std :: vector类时出错

在C ++ 11中,如何最好地在unique_ptr中为普通数组创建迭代器?

在C ++中初始化静态std :: map <int,unique_ptr <int >>

C ++ 11中unique_ptr的向量

C ++ std :: unique_ptr从函数返回并测试null

C ++如何从采用构造函数参数的类中创建std :: unique_ptr

dll中的std :: unique_ptr pimpl用Visual Studio生成C4251

是否可以使用fill构造函数创建std :: vector <std :: unique_ptr <Bar >>?

std :: remove_if来自std :: vector的多态std :: unique_ptr

std :: unique_ptr是否在其析构函数中将其基础指针设置为nullptr?

将std :: vector <std :: unique_ptr <T >>分配给另一个std :: vector <std :: unique_ptr <T >>

使用`std :: unique_ptr`时`std :: vector`中的数据不同

std :: vector <std :: unique_ptr <>> :: push_back()的正确语法是什么?

使用std :: vector <double>访问由std :: unique_ptr <double [2]>管理的数据

如何检查std :: unique_ptr是否为空(如果它位于std :: vector中)?

如何调整std :: vector <std :: queue <std :: unique_ptr <int >>>的大小?

构造类成员std :: vector <std :: unique_ptr <AClass>的智能方法

还有什么更好的替代std :: vector <std :: unique_ptr <T >>吗?

错误C2248:'std :: unique_ptr <_Ty> :: unique_ptr':无法访问在类'std :: unique_ptr <_Ty>'中声明的私有成员

std :: vector <std :: unique_ptr <int>>不编译

C ++ std :: unique_ptr在std :: map中

使用std :: move(nullptr)的unique_ptr的operator =的C ++错误

C ++ 03中std :: unique_ptr的仿真

带有 STL 容器的 C++ std::unique_ptr

'operator[]' 不匹配(操作数类型是 'std::unique_ptr<std::vector<int> >' 和 'int')