Search code examples
c++rect

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.


Solution

  • 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.