conversiion from 'std::_shared_ptr<int,(__gnu_cxx::_Lock_policy)2>::element_type*‘ {aka 'int*'} to non-scalar type 'std::shared_ptr<int>'requested
时间: 2024-03-04 12:49:14 浏览: 125
这个错误通常是因为您将一个指针类型的变量或参数,直接赋值给了 `std::shared_ptr` 类型的智能指针,而这是不允许的。
例如,下面的代码会导致这个错误:
```
int* p = new int(10);
std::shared_ptr<int> ptr = p; // 错误:不能将 int* 类型的指针直接赋值给 std::shared_ptr<int> 类型的智能指针
```
正确的做法是要使用智能指针的构造函数,将指针类型的变量或参数转换为智能指针类型:
```
int* p = new int(10);
std::shared_ptr<int> ptr(p); // 正确:使用 std::shared_ptr<int> 的构造函数将 int* 类型的指针转换为智能指针类型
```
或者,您也可以使用 `std::make_shared` 函数来创建智能指针:
```
int* p = new int(10);
std::shared_ptr<int> ptr = std::make_shared<int>(*p); // 正确:使用 std::make_shared 函数创建智能指针并初始化
```
这样就能够避免这个错误了。
相关问题
conversion from 'std::__shared_ptr<int,(_gnu_cxx::_Lock_policy)2>::element_type*' {aka 'int*'}ton non-
这个错误通常是因为您将一个 `std::shared_ptr` 类型的智能指针,直接赋值给了一个非指针类型的变量或参数。
例如,下面的代码会导致这个错误:
```
std::shared_ptr<int> ptr(new int(10));
int i = ptr; // 错误:不能将 std::shared_ptr<int> 类型的智能指针直接赋值给 int 类型的变量
```
正确的做法是要通过智能指针的 `get()` 成员函数,获取指针类型的值,再赋值给变量或参数:
```
std::shared_ptr<int> ptr(new int(10));
int i = *(ptr.get()); // 正确:通过 get() 函数获取指针类型的值,并赋值给 int 类型的变量
```
或者,您也可以直接将智能指针作为参数传递给接受指针类型参数的函数:
```
void foo(int* p) {
// ...
}
std::shared_ptr<int> ptr(new int(10));
foo(ptr.get()); // 正确:通过 get() 函数获取指针类型的值,并传递给接受指针类型参数的函数
```
这样就能够避免这个错误了。
#0 Homer::RtpSessionState::deprovisionAtDestruction (this=0x7f1fe00e7650) at ../components/mos/state/session_state/src/rtp_session_state.cpp:886 886 ../components/mos/state/session_state/src/rtp_session_state.cpp: No such file or directory. [Current thread is 1 (Thread 0x7f1ffffff700 (LWP 372))] (gdb) bt #0 Homer::RtpSessionState::deprovisionAtDestruction (this=0x7f1fe00e7650) at ../components/mos/state/session_state/src/rtp_session_state.cpp:886 #1 0x00005653759a8e33 in Homer::DeviceState::~DeviceState (this=0x7f1fe00ecf50) at ../components/mos/state/device_state/src/device_state.cpp:87 #2 0x0000565375c4229e in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release (this=0x7f1fe00ecf40) at /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/shared_ptr_base.h:155 #3 std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count (this=0x7f1fe00bbb08) at /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/shared_ptr_base.h:730 #4 std::__shared_ptr<Homer::DeviceStateBase, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr (this=0x7f1fe00bbb00) at /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/shared_ptr_base.h:1169 #5 Homer::Device::~Device (this=0x7f1fe00bb070) at ../components/mos/device/src/device.cpp:94 #6 0x0000565375c3c08a in Homer::WebsocketDevice::~WebsocketDevice (this=0x7f1fe00bb070) at ../components/mos/device/src/websocket_device.h:31 #7 Homer::WebsocketClientDevice::~WebsocketClientDevice (this=0x7f1fe00bb070) at ../components/mos/device/src/websocket_client_device.cpp:20
这是一个程序崩溃的调试信息,使用了gdb进行调试。根据backtrace可以看出,程序崩溃的位置在`Homer::RtpSessionState::deprovisionAtDestruction()`函数内,该函数位于`../components/mos/state/session_state/src/rtp_session_state.cpp`文件的第886行。崩溃的原因可能是访问了不存在的文件或目录,也有可能是访问了空指针或已经被释放的内存等问题。需要对程序进行进一步调试和分析,才能找到具体的原因和解决方案。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)