"attempting to reference a deleted function" 在函数参数上

"attempting to reference a deleted function" on a function argument

我知道还有其他线程出现此错误,但似乎该错误没有一个单一的解决方案,其他线程中的 none 对我的情况有所帮助。

我正在尝试使用 dlib 处理图像。我试过读取图像,访问像素并使用 dlib 保存图像,并且它有效。有效代码,供参考:

#include <cstdlib>
#include <iostream>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <dlib/image_transforms.h>
#include <fstream>

using namespace std;
using namespace dlib;

int main(int argc, char** argv) {
    //reading the image
    array2d<rgb_pixel> img;
    load_image(img, "landscape.bmp");

    //accessing a pixel
    cout << (int)img[0][0].red << "," << (int)img[0][0].green << "," << (int)img[0][0].blue << endl;

    //saving an image
    string name = "landscape2.bmp";
    save_bmp(img, name);

    cout << "press enter to exit..." << endl;
    cin.ignore();
    return 0;
}

现在,我尝试创建一个获取图像并对其进行处理的函数。为了处理它,我想使用我在其他模块中定义的一些其他功能。这是与前面代码相同的 main.cpp 文件中的函数:

#include "basic_preprocessing_alg.h"
#include <vector>

void    create_preprocessed_image(std::string image_name, int image_width = 1280, int image_height = 720, int elem_sim_constant = 1, int new_elem_sim_threshold = 10) {

    //reading the image and saving it to a dlib array named img
    array2d<rgb_pixel> img;
    load_image(img, "landscape.bmp");

    // sending the image to another function to be processed
    std::vector<rgb_pixel> frame = image_to_frame(img);
}

现在我得到了错误。 'std::vector frame = image_to_frame(img);' 中的 'img' 带有下划线,表示 "cannot be referenced -- it is a deleted function".

basic_preprocessing_alg.h 文件包含以下内容:

#include <vector>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <dlib/image_transforms.h>

std::vector<dlib::rgb_pixel> image_to_frame(dlib::array2d<dlib::rgb_pixel> frame);

并且 basic_preprocessing_alg.cpp 包含以下内容:

#include "basic_preprocessing_alg.h"
#include <vector>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <dlib/image_transforms.h>


std::vector<dlib::rgb_pixel> image_to_frame(dlib::array2d<dlib::rgb_pixel> frame) {
    //for now it only returns an empty vector, just for testing
    return std::vector<dlib::rgb_pixel>();
}

我不得不说我是一个 C++ 初学者,我主要使用 Python 编写代码并且最近刚刚尝试过 C++。这可能是一个菜鸟错误或其他什么,但我完全困惑,我在 Google.

上找不到任何有用的东西

我在尝试构建时收到的完整错误消息:

1>c:\users\...\main.cpp(51): error C2280: 'dlib::array2d<dlib::rgb_pixel,dlib::default_memory_manager>::array2d(const dlib::array2d<dlib::rgb_pixel,dlib::default_memory_manager> &)': attempting to reference a deleted function
1>c:\users\...\array2d_kernel.h(163): note: see declaration of 'dlib::array2d<dlib::rgb_pixel,dlib::default_memory_manager>::array2d'
1>c:\users\...\array2d_kernel.h(163): note: 'dlib::array2d<dlib::rgb_pixel,dlib::default_memory_manager>::array2d(const dlib::array2d<dlib::rgb_pixel,dlib::default_memory_manager> &)': function was explicitly deleted

我不明白我尝试引用的函数是什么,但已被删除。我不知道如何解决这个问题或我的代码有什么问题。

我正在使用 VisualStudio 2017 和 Windows 10(如果重要的话)。

错误是关于dlib::array2d的删除复制构造函数:

array2d(const array2d&) = delete;        // copy constructor

需要可访问的复制构造函数才能按值传递参数。通过引用传递 frame 以修复此错误:

std::vector<dlib::rgb_pixel> image_to_frame(dlib::array2d<dlib::rgb_pixel> &frame);
//                                                                         ^

注意:如果您计划只调用 frame 上的 const 函数,请将其传递给 const 引用。