C++ Struct within Struct + deleted function 错误

标签 c++

我在一个项目中有一个头文件,我在其中声明了许多结构。后面的一些结构的成员是早期声明的结构的类型。我知道必须在使用这些成员结构之前对其进行声明,但我遇到了一种奇怪的行为。

在下面的文件中,我尝试将一个新成员添加到 Coordinate 类型的第二个结构 (LUT)。如您所见,底部结构 libraryEntry 有一个类似的成员;这种情况已经有一段时间了,没有造成任何问题。

#ifndef STRUCTS_H
#define STRUCTS_H
#pragma once
#include <string>
#include "Enums.h"

struct Coordinate {
    int X;
    int Y;
    Coordinate(int x, int y) : X(x), Y(y) {}
};
struct LUT {
    int offset;
    std::string hexCode;
    bool modifiedByTrojan;
    //Coordinate xyCoordinate; <======= Causes build to fail when uncommented
};
struct libraryEntry {
    int id;
    DeviceType deviceType;
    int offSet;
    Coordinate xyCoordinate;
    std::string hexCode;
    DeviceConfiguration deviceConfig;
    libraryEntry(int idNum, DeviceType deviceType, int offSet, Coordinate xyCoordinate, std::string hexCode) :
        id(idNum),
        deviceType(deviceType),
        offSet(offSet),
        xyCoordinate(xyCoordinate),
        hexCode(hexCode)
    {

    }
};
#endif

上面添加Coordinate成员报错:

'LUT::LUT(void)':attempting to refernce a deleted function

为什么这只发生在第二个结构中?

最佳答案

在有效的结构 (libraryEntry) 中,您已经定义了一个构造函数,并且在您的构造函数中,您正在使用其双参数构造函数初始化 xyCoordinate

在编译失败的结构中,你没有定义任何构造函数,所以你得到默认的无参数构造函数,它初始化所有东西,包括你的 Coordinate 成员,使用它们的默认值(没有-arg) 构造函数。 Coordinate 没有默认构造函数,因为您声明了一个不同的构造函数,这会导致默认构造函数被删除,因此会出现错误消息。

关于C++ Struct within Struct + deleted function 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38471979/

相关文章:

c++ - 将 PIMPL 与单例和 auto_ptr 结合使用

java - 将 CPP 应用程序移植到 Android

c++ - 在 C++ 中以字节序列存储整数值

c++ - 当检查单元格是否被占用时 CLI/C++ 俄罗斯方 block block 崩溃 MSVisualStudio 2008

c++ - 为什么要私下继承基类,还要名字publicizing?

c++ - 临时实例的常量正确性

c++ - Outlook 2010 C++ 加载项 - HTML 电子邮件正文检索

c++ - 使用 cURL 在 C++ 中将网页保存到内存

c++ - 为什么 C++ 中不允许从 int (*)(int) 到 void* 的 static_cast ?

我的教授无法弄清楚的 C++ 循环错误