简体   繁体   中英

C++ passing std::shared_ptr<RECT> through function

void PlayerHealthBar::SetSourceRect(std::shared_ptr<RECT> sourceRect)
{
    this->sourceRect = sourceRect;
}

.CPP file I am trying to set the source rect from

playerHealthBar->SetSourceRect(std::shared_ptr<RECT>(0.0, 0.0, 0.0, 0.0));

The error is on the .cpp file on shared_ptr<RECT> saying:

8   IntelliSense: no instance of constructor "std::shared_ptr<_Ty>::shared_ptr [with _Ty=RECT]" matches the argument list
            argument types are: (double, double, double, double)    ...\Ship.cpp    84

I am not sure what this means. Thanks.

You should provide a dynamically allocated pointer to std::shared_ptr 's constructor. Alternatively, you can also, and I'd recommend it, use the "factory function" std::make_shared , as follows:

playerHealthBar->SetSourceRect(std::make_shared<RECT>(0.0, 0.0, 0.0, 0.0));
//                                  ^^^^^^^^^^^

Of course assuming RECT accepts 4 double-literal-convertible types in one of its constructors.

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