note: #39;person::person()#39; is implicitly deleted because the default definition would be ill-formed(注意: person::person() 被隐式删除,因为默认定义格式不正确)
问题描述
我正在开发一个示例程序来帮助我学习 C++ 中的结构.这是我的代码:
I'm working on an example program to help me learn structs in C++. Here's my code:
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
int nextPersonID = 0;
int nextAddressID = 0;
struct date {
int day;
int month;
int year;
};
struct address {
int id;
string address;
date effectiveDate;
date expirationDate;
};
struct person {
int id;
string name;
date birthdate;
const int numberOfAddresses;
address addresses [1];
};
int main () {
person bob;
bob.name = "Bob";
bob.id = nextPersonID;
nextPersonID++;
bob.birthdate.day = 1;
bob.birthdate.month = 1;
bob.birthdate.year = 1990;
bob.numberOfAddresses = 1;
bob.addresses[0].address = "31415 E. Pi Blvd.";
bob.addresses[0].id = nextAddressID;
nextAddressID++;
bob.addresses[0].effectiveDate.day = 1;
bob.addresses[0].effectiveDate.month = 1;
bob.addresses[0].effectiveDate.year = 1990;
bob.addresses[0].expirationDate.day = 1;
bob.addresses[0].expirationDate.day = 1;
bob.addresses[0].expirationDate.day = 2020;
cout << bob.name;
}
但是当我尝试编译时,它失败了 注意:'person::person()' 被隐式删除,因为默认定义格式不正确.
.这是我的构建日志:
But when I try to compile, it fails with note: 'person::person()' is implicitly deleted because the default definition would be ill-formed.
. Here's my build log:
-------------- Build: Debug in DataStructures (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -Wall -g -std=c++11 -I"C:Program Files (x86)CodeBlocksMinGW_Dev_LibsincludeSDL2" -c C:UsersDuncanDocumentsC++ChallengesDataStructuresDataStructures.cpp -o objDebugDataStructures.o
C:UsersDuncanDocumentsC++ChallengesDataStructuresDataStructures.cpp: In function 'int main()':
C:UsersDuncanDocumentsC++ChallengesDataStructuresDataStructures.cpp:32:12: error: use of deleted function 'person::person()'
C:UsersDuncanDocumentsC++ChallengesDataStructuresDataStructures.cpp:23:8: note: 'person::person()' is implicitly deleted because the default definition would be ill-formed:
C:UsersDuncanDocumentsC++ChallengesDataStructuresDataStructures.cpp:23:8: error: uninitialized non-static const member 'const int person::numberOfAddresses'
C:UsersDuncanDocumentsC++ChallengesDataStructuresDataStructures.cpp:39:29: error: assignment of read-only member 'person::numberOfAddresses'
Process terminated with status 1 (0 minute(s), 1 second(s))
3 error(s), 0 warning(s) (0 minute(s), 1 second(s))
我在 Google 上找不到任何与我的问题相关的信息.有任何想法吗?我正在使用带有 g++ 的 Code::Blocks.
I can't find anything with Google relating to my problem. Any ideas? I'm using Code::Blocks with g++.
推荐答案
好吧,问题不在于那个注释".注释"只是解释了错误的原因.错误是当类 person
没有默认构造函数时,您试图默认构造您的 person
对象.
Well, the problem is not with that "note". The "note" simply explains the reason for the error. The error is that you are trying to default-construct your person
object when class person
does not have a default constructor.
您可以{}
- 初始化该 const 成员,然后代码将编译
Instead of trying to default-construct it, you can {}
- initialize that const member and the code will compile
person bob = { nextPersonID++, "Bob", {}, 1 };
bob.birthdate.day = 1;
bob.birthdate.month = 1;
bob.birthdate.year = 1990;
...
或者,您可以简单地为该类编写自己的默认构造函数.
Alternatively, you can simply write your own default constructor for the class.
这篇关于注意: 'person::person()' 被隐式删除,因为默认定义格式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:注意: 'person::person()' 被隐式删除,因为默认
基础教程推荐
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01