分配 tr1::shared_ptr 时遇到问题

发布于 2024-09-03 23:53:06 字数 730 浏览 2 评论 0原文

我有一个类,其中有 tr1::shared_ptr 作为成员,如下所示:

class Foo
{
    std::tr1::shared_ptr<TCODBsp> bsp;

    void Bar();
}

在成员函数 Bar 中,我尝试像这样分配它:

bsp = newTCODBsp(x,y,w,h);< /code>

g++ 然后给我这个错误

no match for 'operator=' in '((yarl::mapGen::MapGenerator*)this)->yarl::mapGen::MapGenerator::bsp = (operator new(40u), (<语句>, ((TCODBsp*)<匿名>)))' /usr/include/c++/4.4/tr1/shared_ptr.h:834:注意:候选者是: std::tr1::shared_ptr& std::tr1::shared_ptr::operator=(const std::tr1::shared_ptr&)

在我的代码中, Foo 实际上是 yarl::mapGen::MapGenerator。我做错了什么?

I've got a class that has a tr1::shared_ptr as a member, like so:

class Foo
{
    std::tr1::shared_ptr<TCODBsp> bsp;

    void Bar();
}

In member function Bar, I try to assign it like this:

bsp = newTCODBsp(x,y,w,h);

g++ then gives me this error

no match for ‘operator=’ in ‘((yarl::mapGen::MapGenerator*)this)->yarl::mapGen::MapGenerator::bsp = (operator new(40u), (<statement>, ((TCODBsp*)<anonymous>)))’
/usr/include/c++/4.4/tr1/shared_ptr.h:834: note: candidates are: std::tr1::shared_ptr<TCODBsp>& std::tr1::shared_ptr<TCODBsp>::operator=(const std::tr1::shared_ptr<TCODBsp>&)

in my code, Foo is actually yarl::mapGen::MapGenerator. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡看悲欢离合 2024-09-10 23:53:06

调用.reset(new TCODBsp)或说bsp = std::tr1::shared_ptr(new TCODBsp)。共享指针是显式的。您不能只将 ptr 类型分配给它们。

call .reset(new TCODBsp) or say bsp = std::tr1::shared_ptr(new TCODBsp). Shared pointers are explicit. You can't just assign the ptr type to them.

九命猫 2024-09-10 23:53:06

您不能将本机指针分配给共享指针。必须使用该值初始化shared_ptr,或者您可以使用本机指针值调用reset()。

You can't assign a native pointer to a shared pointer. The shared_ptr must be initialized with that value, or you can call reset() with the native pointer value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文