重新分配当前为 std::unique_ptr 的自动类型变量

Reassign variable of type auto that is currently std::unique_ptr

我目前正在试验 ESP32-CAM,这是一款带有集成摄像头的微控制器,您可以通过 Arduino IDE(使用 C++11)进行编程。为了使用 cam 捕获图像,有一个名为 'esp32cam' 的库,其中包含函数 esp32cam::capture()。这个函数显然 returns 一个类型为 std::unique_ptr<esp32cam::Frame> 的变量。还有一个示例草图,它将返回的帧保存在 auto:

类型的局部变量中
auto frame = esp32cam::capture();

不过对于我的项目,我想全局存储摄像头拍摄的最后一张图像,所以我尝试了这个:

auto lastPic = std::unique_ptr<esp32cam::Frame>();
void takePicture() {
  lastPic = esp32cam::capture();
}

但是,这给了我以下错误:

use of deleted function 'std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = esp32cam::Frame; _Dp = std::default_delete<esp32cam::Frame>]'

我是 C++ 的新手,所以我不太明白这意味着什么,但就我而言 std:unique_ptr 管理一个对象并在它不再需要它时处理它,并且因为我试图向变量添加一个不同的对象,所以它基本上是一个不同的 unique_ptr,因此也是一个不同的类型,所以我无法分配它。 也很可能是我理解完全错误。

所以我的问题是:如何重新分配这个变量?

感谢任何帮助;提前致谢。

尝试改变这个:

auto lastPic = std::unique_ptr<esp32cam::Frame>();
void takePicture() {
  lastPic = esp32cam::capture();
}

对此:

auto lastPic = std::unique_ptr<esp32cam::Frame>();
void takePicture() {
  lastPic = std::move (esp32cam::capture());
//          ^^^^^^^^^
}

(因为std::unique_ptr可移动但不可复制。)