0
votes

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 ?

3
Apart from anything else the name __elevi is illegal in C++ user code, as are all names that contain double underscores or start with an underscore and an uppercase letter. You also need to post the code that calls your function, and to investigate the concept of references. - user2100815
Please use the { } button in the text editor to format your code. You get better answers when your question is readable. - jalf
Yes, nothing weird. Have a look at your shared_ptr initialisation. - Julio Guerra
Are you using a C++1x compiler ? Also checking the Scoala object pointer would be a good idea here. - BatchyX
How are you creating the shared_ptr's? The location in the message looks very much like an uninitialized pointer plus an offset. - Bo Persson

3 Answers

1
votes

Most likely your elev object passed into adaugaElev was either not allocated, or was somehow unallocated (reference count hit zero in the shared_ptr) - you'll need to post more code showing how you allocate your Elev object to confirm that.

0
votes

Run your program under valgrind - it will help illuminate where the problems are.

-1
votes

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.