【问题标题】:note: 'person::person()' is implicitly deleted because the default definition would be ill-formed注意: 'person::person()' 被隐式删除,因为默认定义格式不正确
【发布时间】:2015-09-20 15:55:28
【问题描述】:

我正在开发一个示例程序来帮助我学习 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;
}

但是当我尝试编译时,它以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))

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

【问题讨论】:

  • 来自您自己的错误:uninitialized const member 'const int person::numberOfAddresses'
  • 哦,声明后不能定义const?
  • 你必须在声明它的同时用一个值初始化一个常量: const int x = 0;不是常量 x; x = 0;

标签: c++ struct g++


【解决方案1】:

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

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

person bob = { nextPersonID++, "Bob", {}, 1 };
bob.birthdate.day = 1;
bob.birthdate.month = 1;
bob.birthdate.year = 1990;
...

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

【讨论】:

  • 好的,谢谢。但另外一件事,我首先使用 const 的原因是试图允许用户输入数组大小。这个解决方案可以让我这样做吗?
  • @VirtualDXS:好吧,如果你做得好,它会的。但是const 与促进“用户输入”无关。坦率地说,我认为该字段没有理由成为 const
  • @VirtualDXS:“同样的错误”?我在您的原始帖子中没有看到这样的错误。在这种情况下,它有什么“用途”?
  • @VirtualDXS:“什么用途”我的意思是:哪一行代码触发了这个错误?
  • 感谢您的提示! const 成员触发了这个问题
【解决方案2】:

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

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)
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 2022-01-22
    相关资源
    最近更新 更多