不存在合适的构造函数来从“哑指针”转换为“哑指针”。到“智能指针”
struct A
{
A(int a);
};
struct B
{
B();
void b(std::shared_ptr<A> a);
};
int main()
{
A a(1);
B b;
b.b(&a);
}
所以我收到了这个错误,抱歉,这是我第一次使用智能指针!
错误:
不存在合适的构造函数可从
"A *"
转换为"std::tr1::shared_ptr"
我该如何解决此问题!?
struct A
{
A(int a);
};
struct B
{
B();
void b(std::shared_ptr<A> a);
};
int main()
{
A a(1);
B b;
b.b(&a);
}
So I got this error, sorry guys it's my frist time with the smart pointers!!
Error:
no suitable constructor exists to convert from
"A *"
to"std::tr1::shared_ptr<A>"
How do I fix this problem!?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
智能指针的全部要点就是拥有所有权。也就是说,它负责释放它所指向的任何内容。试图告诉它管理已经由完全不同的系统管理的东西根本没有意义。
在您的情况下,
a
已经被自动管理,为什么您还希望也 由智能指针管理?即使这有效,你也只是将自己设置为删除它两次,这就是 UB。要么给它一些东西来拥有,比如
new A(1)
,或者改变b
来操作它不拥有的东西。The entire point of a smart pointer is to have ownership. That is, it's responsible for the deallocation of whatever it's pointing it. It simply doesn't make sense to try to tell it to manage something that's already being managed by a completely different system.
In your case,
a
is already being automatically managed, why would you want to also be managed by a smart pointer? Even if that worked, you'd just be setting yourself for deleting it twice, which is UB.Either give it something to own, like
new A(1)
, or changeb
to operate on something it doesn't own.其他人已经抱怨了代码的设计错误,但并不是代码无法编译的真正问题。
shared_ptr
有一个接受原始指针的构造函数,但它被标记为explicit
,这意味着您必须显式写出您想要构造一个shared_ptr< /代码> 实例。您的函数调用尝试的是隐式地进行该构造,由于显式关键字,这是不允许的。
以下代码将编译,但会给出未定义的行为,因为
shared_ptr
将(尝试)删除
驻留在堆栈上且不可删除的对象:< code>shared_ptr 是你可以向构造函数传递一个删除器,当应该删除拥有的指针时将调用该删除器。您可以编写并使用“noop”删除器,它什么也不做;以下不会调用未定义的行为,也不会尝试删除堆栈变量:
如果您有一个绝对需要
shared_ptr
的库 API,但您想调用它,那么实际上这是有用途的带有堆栈变量。但该 API 的设计是另一回事......Others already ranted on the design error of your code, but not the real problem why the code doesn't even compile.
shared_ptr
has a constructor that accepts a raw pointer, but it is marked asexplicit
, which means you have to explicitly write out that you want to construct ashared_ptr
instance. What your function call tries, is to do that construction implicitly, which isn't allowed because of the explicit keyword.The following will compile but give undefined behaviour because the
shared_ptr
will (try to)delete
an object which resides on the stack and is as such not deleteable:A special trait of
shared_ptr
is that you can pass the constructor a deleter, which will be called when the owned pointer should be deleted. You can just write and use a "noop" deleter, which does just nothing; the following will not invoke undefined behaviour and will not try to delete the stack variable:And there actually is a use for this, if you have a library API that absolutely wants a
shared_ptr
but you want to call it with a stack variable. The design of that API is another thing though...std::tr1::shared_ptr 有一个构造函数,允许传递给定的原始指针。因此,如果您有一个指向 A 的指针,您会执行以下操作:
std::shared_ptr (pMyA)
但在您的情况下,指向 A 的指针指向一个自动变量,而不是指向可以在使用后删除的动态分配的内存资源。
像这样的东西将是一个更好的用例:
std::tr1::shared_ptr has one constructor that allows to pass down a given raw pointer. So if you had a pointer to A, you would do something like:
std::shared_ptr (pMyA)
but in your case, your pointer to A points to an automatic variable NOT to a dynamically allocated memory resource that can be deleted after usage.
Something like this would be a better use case: