简体   繁体   English

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

[英]C++ - std::unique_ptr in vector<> is nullptr

I want to store Particle objects in a vector object, so I can access it later. 我想将Particle对象存储在vector对象中,以便稍后可以访问它。 These particles ( Electrons and Protons ) are inherited from the Particle class which contains a toString() virtual method. 这些粒子( ElectronsProtons )继承自包含toString()虚方法的Particle类。 This toString() method is then overrided in Electron and Proton classes. 然后在ElectronProton类中重写此toString()方法。

When I read the vector container, I want to access to the toString() method specific to Electron or Proton , and not to Particle . 当我读取矢量容器时,我想访问特定于ElectronProtontoString()方法,而不是Particle

Apparently, one way is to use std::unique_ptr . 显然,一种方法是使用std::unique_ptr Here is the part of the code I try to run: 这是我尝试运行的代码的一部分:

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
    /**/
}

A pointer to a Particle object seems to be fine, but not to an Electron or Proton . 指向Particle对象的指针看起来很好,但不适用于ElectronProton I guess there is something wrong with the constructors ? 我猜构造函数有问题吗?

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;
}

and the definitions: 和定义:

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){}

You've made a classic mistake that trips up even the most experienced C++ programmers: you declared the vector with an initial size and then push_back ed additional elements to it instead of assigning to the existing elements. 你犯了一个经典的错误,即使是最有经验的C ++程序员也会绊倒:你用初始大小声明了向量,然后将其他元素push_back给它而不是分配给现有的元素。 Fix this by removing the (nbParticles) from the vector initialization. 通过从向量初始化中删除(nbParticles)来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM