57

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

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

3
  • 7
    From your own error: uninitialized const member 'const int person::numberOfAddresses' Jul 2, 2015 at 23:18
  • Oh, you can't define a const after declaring it? Jul 2, 2015 at 23:23
  • 1
    You must initialize a const with a value at the same time you declare it: const int x = 0; NOT const x; x = 0; Feb 17, 2018 at 13:18

2 Answers 2

37

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.

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.

6
  • Okay, thanks. But one other thing, the reason I was using a const in the first place was to try to allow for a user to input the array size. Would this solution allow me to do that? Jul 3, 2015 at 0:14
  • @VirtualDXS: Well, it would, if you do it properly. But const has nothing to do with facilitating "user input". Frankly, I see no reason at all for that field to be const. Jul 3, 2015 at 0:17
  • @VirtualDXS: "The same error"? I don't see such error in your original post. What "use" is it in this case? Jul 3, 2015 at 0:31
  • @VirtualDXS: By "what use" I mean: what specific line of code triggers this error? Jul 3, 2015 at 1:18
  • Thanks for the hint! The const member triggered this issue
    – MewX
    Mar 26, 2018 at 15:51
6

The problem has not to do with the default-constructor. There is a constant in the class declaration but the default (no argument) constructor 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)
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.