2015-07-02 12 views
16

Pracuję nad przykładowym programem, który pomoże mi uczyć się struktur w C++. Oto mój kod:uwaga: "osoba :: osoba()" została domyślnie usunięta, ponieważ domyślna definicja byłaby źle sformułowana

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

Ale gdy próbuję skompilować, nie jest on z note: 'person::person()' is implicitly deleted because the default definition would be ill-formed.. Oto mój dziennik kompilacji:

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

Nie mogę znaleźć nic w Google w związku z moim problemem. Jakieś pomysły? Używam Code :: Blocks z g ++.

+4

Z własnego błędu: 'niezonitalizowany element const 'const int person :: numberOfAddresses'' –

+0

Och, nie możesz zdefiniować stałej po jej zadeklarowaniu? –

+0

Należy zainicjować stałą o wartości w momencie jej zadeklarowania: const int x = 0; NOT const x; x = 0; – JRFerrell

Odpowiedz

16

Cóż, problem nie dotyczy tej "notatki". "Uwaga" po prostu wyjaśnia przyczynę błędu. Błąd polega na tym, że próbujesz utworzyć domyślny obiekt person, gdy klasa person nie ma domyślnego konstruktora.

Zamiast próbować default-skonstruować go można {} - zainicjować ten członek const a kod zostanie skompilowany

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

Alternatywnie, można po prostu napisać własny domyślnego konstruktora dla klasy.

+0

Dobra, dzięki. Ale jeszcze jedno, powód, dla którego używałam const w pierwszej kolejności, polegał na próbie umożliwienia użytkownikowi wprowadzenia rozmiaru tablicy. Czy to rozwiązanie pozwoli mi to zrobić? –

+0

@VirtualDXS: Cóż, zrobiłby to, jeśli zrobisz to poprawnie. Ale 'const' nie ma nic wspólnego z ułatwianiem" wprowadzania danych przez użytkownika ". Szczerze mówiąc, nie widzę żadnego powodu, aby to pole było "const". – AnT

+0

@VirtualDXS: "Ten sam błąd"? Nie widzę takiego błędu w Twoim oryginalnym wpisie. Jakie jest "użycie" w tym przypadku? – AnT

Powiązane problemy