gpt4 book ai didi

C++ Struct within Struct + deleted function 错误

转载 作者:行者123 更新时间:2023-11-27 22:49:48 43 4
gpt4 key购买 nike

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

在下面的文件中,我尝试将一个新成员添加到 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/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com