【问题标题】:Error C2280 : Class::Class(void) : Attempting to reference a deleted function错误 C2280 : Class::Class(void) : 试图引用已删除的函数
【发布时间】:2021-11-13 01:54:22
【问题描述】:

所以,我正在处理一个项目,我在这个项目中有两个文件: main.cpp、matrix.h

问题是我的代码在几个小时前似乎可以正常工作,现在却不行了

main.cpp:

#include <iostream>
#include "matrix.h"
#include <vector>
int main() {
    Matrix f;
    f.create(10, 1, {3, 4, 5, 6, 7, 8, 9});
}

矩阵.h:

#pragma once
#include <iostream>
#include <string>
#include <Windows.h>
#include <vector>
class Matrix {
public:
    const size_t N;
    bool ifMatrixCreated;
    const char* NOTENOUGH = "The size of the array should match to the width*height elements";
    std::vector<int> arr;
    int w, h;
    void create(int width, int height, const std::vector<int> a) {
        w = width;
        h = height;
        if (a.size() != width * height) {
            ifMatrixCreated = false;
            std::cout << "bello";
        }
        else {
            ifMatrixCreated = true;
            arr = a;
            std::cout << "hello";
        }
    }
};

当我编译时,它会产生这个错误(使用 VS2019):

Error   C2280 | 'Matrix::Matrix(void)': attempting to reference a deleted function  Matrix | Line 5

老是说“Matrix的默认构造函数不能被引用——它是一个被删除的函数”

你能帮忙解决这个错误吗?

提前致谢。

【问题讨论】:

    标签: c++ visual-studio compiler-errors visual-studio-2019


    【解决方案1】:

    这是正确的工作示例。发生错误是因为必须初始化每个 const 数据成员。和

    如果以下任何一项为真,则类 T 的隐式声明或默认的默认构造函数未定义(C++11 前)定义为已删除(C++11 起):

    • T 有一个 const 成员,没有用户定义的默认构造函数或大括号或等号初始值设定项(C++11 起)。
    #pragma once
    #include <iostream>
    #include <string>
    //#include <Windows.h>
    #include <vector>
    class Matrix {
    public:
        //const size_t N;//this const data member must be initialised
        const size_t N = 6;
        bool ifMatrixCreated;
        const char* NOTENOUGH = "The size of the array should match to the width*height elements";
        std::vector<int> arr;
        int w, h;
        void create(int width, int height, const std::vector<int> a) {
            w = width;
            h = height;
            if (a.size() != width * height) {
                ifMatrixCreated = false;
                std::cout << "bello";
            }
            else {
                ifMatrixCreated = true;
                arr = a;
                std::cout << "hello";
            }
        }
    };
    

    【讨论】:

    • 谢谢你的回答,你摇滚!
    猜你喜欢
    • 2020-08-21
    • 2014-02-19
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    相关资源
    最近更新 更多