简体   繁体   中英

C++ std::vector<std::shared_ptr>

I have a problem with the vector of shared_ptrS. My code is :

//--------scoala.h--------

class Scoala
{
public:
    Scoala(std::string );

    int adaugaElev(std::shared_ptr<Elev> elev);
private:

    std::vector<std::shared_ptr <Elev>> __elevi;
};



//-----------scoala.cpp-----------------------------

int Scoala::adaugaElev(std::shared_ptr<Elev> elev)
{
    __elevi.push_back(elev);
    return __elevi.size() - 1;
}

I run it and I got this "Unhandled exception at 0x0137b559 in elev.exe: 0xC0000005: Access violation reading location 0xcccccd0c". What's wrong ?

您传递给adaugaElev的elev对象很可能没有分配,或者以某种方式未分配(引用计数在shared_ptr中为零)-您需要发布更多代码,以显示如何分配Elev对象以确认这一点。

valgrind下运行程序-这将有助于阐明问题所在。

Using double underscore prefixed names is undefined behaviour in C++. In addition, Elev could be busted for various reasons, and I'd trace it's origin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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