Ich versuche, ein leeres Array von Objekten der Klasse Kontakt zu erstellen. Beginnend mit einem leeren Array, möchte ich eine Funktion in AddrBook.cpp erstellen, um Objekte des Kontakts zum Objekt-Array hinzuzufügen, namens addressBook.Wie initialisiere ich ein leeres Array von Objekten in C++?
Initialisiere ich das Array in AddrBook.h richtig?
Wie überprüfe ich, ob ein Objekt des Kontakts in einem bestimmten Index vorhanden ist?
AddrBook.cpp
#include "AddrBook.h"
namespace address_book_test
{
const int CAPACITY = 5;
void AddrBook::addContact(Contact& itemToAdd) // Add Contact to the AddrBook (using Contact object)
{
for (int i = 0; i < CAPACITY; i++)
{
if (/*Contact object does not exist at i*/)
{
/*Add Contact object*/
return;
}
}
return;
}
...
}
AddrBook.h
#ifndef ADDR_BOOK_H
#define ADDR_BOOK_H
#include <fstream>
using namespace std;
#include "Contact.h"
namespace address_book_test
{
class AddrBook
{
public:
static const int CAPACITY = 5;
// CONSTRUCTOR
AddrBook() { used = 0; }
// Modification Member Functions
void addContact(Contact& itemToAdd); // Add Contact to the AddrBook (using Contact object)
...
private:
static Contact addressBook[CAPACITY]; // The array used to store Contact objects
int used; // How much of addressBook is used
};
}
#endif
Contact.cpp
#ifndef CONTACT_H
#define CONTACT_H
#include <fstream>
using namespace std;
#include "Address.h"
#include "Name.h"
namespace address_book_test
{
class Contact
{
public:
// Constructor
Contact(string inLastName = "",
string inFirstName = "",
string inStreetAddress = "",
string inCity = "",
string inState = "",
string inZip = "",
string inPhone = "",
string inEmail = "",
string inBirthday = "",
string inPictureFile = "")
{
Name(inLastName, inFirstName);
Address(inStreetAddress, inCity, inState, inZip);
setPhone(inPhone);
setEmail(inEmail);
setBirthday(inBirthday);
setPictureFile(inPictureFile);
}
...
private:
Name fullName;
Address fullAddress;
string phone;
string email;
string birthday;
string pictureFile;
};
}
#endif
Arrays haben eine feste Größe. Sie können kein leeres Array haben. Es hat immer genau die "KAPAZITÄT" Anzahl der Elemente. Wenn Sie die Größe ändern möchten, verwenden Sie einen 'std :: vector'. – nwp
Es existiert bereits ein Objekt an der Position "i". Sie können es nur mit einer Kopie überschreiben. – user0042