Boost shared pointer initialization

2019-08-10 00:34发布

问题:

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>());