Boost shared pointer initialization

Deadly 提交于 2020-01-15 10:33:29

问题


I am pretty a beginner in C++. I have following problem. In the class MevisPatientModel I defined:

typedef boost::shared_ptr<egMevisPatientModel> Ptr_t;

Then I "defined" the variable:

egMevisPatientModel::Ptr_t v_PatientModel;

Now when I try to access the getType function of the class MevisPatientModel :

v_PatientModel->getType()

...I'm getting following message:

Assertion failed! ...shared_ptr.hpp Expression px!= 0

My intention is that the pointer wasn't initialized. Now I know that here are many answers which would "fit" to my question. But as I said, I am a beginner. To be honest I don't these answers understand. Please help me and give me a clear and specific answer to my problem. Thank you very much.

Greets, Marco


回答1:


You need to create an dynamic object with new and assign the resulting pointer into the shared_ptr:

egMevisPatientModel::Ptr_t v_PatientModel(
    std::make_shared<egMevisPatientModel>());


来源:https://stackoverflow.com/questions/14259740/boost-shared-pointer-initialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!