Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Size of vector



  • I have declared
    std::unique_ptr<std::vector<std::unique_ptr<Album>>> m_albums;
    but when I try to find the number of elements by calling
    m_albums->size();
    it leads to a crash, presumably because no memory is not yet allocated for m_albums. What is the correct way out of this, so that I can call
    m_albums->size();
    even before adding any elements to the the vector?


  • Lifetime Qt Champion

    C++ Basics - you forgot to initialize your shared_ptr.
    But why to use a shared_ptr in the first place here?


Log in to reply