分配 tr1::shared_ptr 时遇到问题
我有一个类,其中有 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
在我的代码中, 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
调用
.reset(new TCODBsp)
或说bsp = std::tr1::shared_ptr(new TCODBsp)
。共享指针是显式的。您不能只将 ptr 类型分配给它们。call
.reset(new TCODBsp)
or saybsp = std::tr1::shared_ptr(new TCODBsp)
. Shared pointers are explicit. You can't just assign the ptr type to them.您不能将本机指针分配给共享指针。必须使用该值初始化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.