C++ 通过函数传递 std::shared_ptr<RECT>

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

本文关键字:lt RECT gt ptr std C++ shared 函数      更新时间:2023-10-16
void PlayerHealthBar::SetSourceRect(std::shared_ptr<RECT> sourceRect)
{
    this->sourceRect = sourceRect;
}

.CPP 文件我正在尝试从中设置源矩形

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

错误出在shared_ptr<RECT>上的.cpp文件上,上面写着:

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

我不确定这意味着什么。谢谢。

你应该提供一个动态分配的指针,指向std::shared_ptr 的构造函数。或者,您也可以(我推荐它)使用"工厂功能"std::make_shared,如下所示:

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

当然,假设RECT在其构造函数之一中接受 4 个双文本可转换类型。