• <bdo id='agZyJ'></bdo><ul id='agZyJ'></ul>

    <small id='agZyJ'></small><noframes id='agZyJ'>

      1. <legend id='agZyJ'><style id='agZyJ'><dir id='agZyJ'><q id='agZyJ'></q></dir></style></legend>
      2. <tfoot id='agZyJ'></tfoot>

      3. <i id='agZyJ'><tr id='agZyJ'><dt id='agZyJ'><q id='agZyJ'><span id='agZyJ'><b id='agZyJ'><form id='agZyJ'><ins id='agZyJ'></ins><ul id='agZyJ'></ul><sub id='agZyJ'></sub></form><legend id='agZyJ'></legend><bdo id='agZyJ'><pre id='agZyJ'><center id='agZyJ'></center></pre></bdo></b><th id='agZyJ'></th></span></q></dt></tr></i><div id='agZyJ'><tfoot id='agZyJ'></tfoot><dl id='agZyJ'><fieldset id='agZyJ'></fieldset></dl></div>

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

        时间:2023-06-30

            <i id='fHm6y'><tr id='fHm6y'><dt id='fHm6y'><q id='fHm6y'><span id='fHm6y'><b id='fHm6y'><form id='fHm6y'><ins id='fHm6y'></ins><ul id='fHm6y'></ul><sub id='fHm6y'></sub></form><legend id='fHm6y'></legend><bdo id='fHm6y'><pre id='fHm6y'><center id='fHm6y'></center></pre></bdo></b><th id='fHm6y'></th></span></q></dt></tr></i><div id='fHm6y'><tfoot id='fHm6y'></tfoot><dl id='fHm6y'><fieldset id='fHm6y'></fieldset></dl></div>
              <tbody id='fHm6y'></tbody>

                • <bdo id='fHm6y'></bdo><ul id='fHm6y'></ul>

                  <small id='fHm6y'></small><noframes id='fHm6y'>

                • <legend id='fHm6y'><style id='fHm6y'><dir id='fHm6y'><q id='fHm6y'></q></dir></style></legend>
                • <tfoot id='fHm6y'></tfoot>
                  本文介绍了注意: '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;
                  }
                  

                  但是当我尝试编译时,它失败了 note: '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()' 被隐式删除,因为默认定义格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:迭代结构;在 RichEdit 框中轻松显示结构字段和值 下一篇:如何在不传递类实例的情况下从静态成员函数调用非静态成员函数

                  相关文章

                • <legend id='fDQjP'><style id='fDQjP'><dir id='fDQjP'><q id='fDQjP'></q></dir></style></legend>

                  <small id='fDQjP'></small><noframes id='fDQjP'>

                • <i id='fDQjP'><tr id='fDQjP'><dt id='fDQjP'><q id='fDQjP'><span id='fDQjP'><b id='fDQjP'><form id='fDQjP'><ins id='fDQjP'></ins><ul id='fDQjP'></ul><sub id='fDQjP'></sub></form><legend id='fDQjP'></legend><bdo id='fDQjP'><pre id='fDQjP'><center id='fDQjP'></center></pre></bdo></b><th id='fDQjP'></th></span></q></dt></tr></i><div id='fDQjP'><tfoot id='fDQjP'></tfoot><dl id='fDQjP'><fieldset id='fDQjP'></fieldset></dl></div>
                  1. <tfoot id='fDQjP'></tfoot>

                        <bdo id='fDQjP'></bdo><ul id='fDQjP'></ul>