C++ 注意: 'person::person()' 被隐式删除,因为默认定义格式错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31196102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:53:12  来源:igfitidea点击:

note: 'person::person()' is implicitly deleted because the default definition would be ill-formed

c++structg++

提问by Duncan X Simpson

I'm working on an example program to help me learn structs in C++. Here's my code:

我正在编写一个示例程序来帮助我学习 C++ 中的结构。这是我的代码:

#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;
}

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:

但是当我尝试编译时,它失败了note: 'person::person()' is implicitly deleted because the default definition would be ill-formed.。这是我的构建日志:

-------------- Build: Debug in DataStructures (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall -g -std=c++11 -I"C:\Program Files (x86)\CodeBlocks\MinGW_Dev_Libs\include\SDL2" -c C:\Users\Duncan\Documents\C++\Challenges\DataStructures\DataStructures.cpp -o obj\Debug\DataStructures.o
C:\Users\Duncan\Documents\C++\Challenges\DataStructures\DataStructures.cpp: In function 'int main()':
C:\Users\Duncan\Documents\C++\Challenges\DataStructures\DataStructures.cpp:32:12: error: use of deleted function 'person::person()'
C:\Users\Duncan\Documents\C++\Challenges\DataStructures\DataStructures.cpp:23:8: note: 'person::person()' is implicitly deleted because the default definition would be ill-formed:
C:\Users\Duncan\Documents\C++\Challenges\DataStructures\DataStructures.cpp:23:8: error: uninitialized non-static const member 'const int person::numberOfAddresses'
C:\Users\Duncan\Documents\C++\Challenges\DataStructures\DataStructures.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))

I can't find anything with Google relating to my problem. Any ideas? I'm using Code::Blocks with g++.

我无法通过 Google 找到与我的问题相关的任何信息。有任何想法吗?我正在使用带有 g++ 的 Code::Blocks。

采纳答案by AnT

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 personobject when class persondoes not have a default constructor.

好吧,问题不在于那个“注释”。“注释”只是解释了错误的原因。错误是person当类person没有默认构造函数时,您试图默认构造对象。

Instead of trying to default-construct it, you can {}- initialize that const member and the code will compile

而不是尝试默认构造它,您可以{}- 初始化该 const 成员,代码将编译

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.

或者,您可以简单地为该类编写自己的默认构造函数。

回答by Quinn Carver

The problem has not to do with a "default-construct ... when class person does not have a default constructor." The problem has to do with having a constant in the declaration of the class and a constructor that does not guarantee that the constant will be defined. Suggest using an "initializer list".

问题与“默认构造......当类人没有默认构造函数时”有关。问题与在类的声明中有一个常量和一个不能保证定义常量的构造函数有关。建议使用“初始化列表”。

struct Person {
        int id;
        string name;
        date birthdate;
        const int numberOfAddresses;
        address addresses [1];

        Person(int); // constructor declaration
        Person() : numberOfAddresses(1) {} // constructor definition.
                      // ": numberOfAddresses(1)" is the initializer list
                      // ": numberOfAddresses(1) {}" is the function body
    };
    Person::Person(int x) : numberOfAddresses(x) {} // constructor definition. ": numberOfAddresses{x}" is the initializer list
    int main()
    {
        Person Bob; // calls Person::Person()
        Person Shurunkle(10); // calls Person::Person(int)
    }