2017-09-17 3 views
1

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 
+1

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

+0

Es existiert bereits ein Objekt an der Position "i". Sie können es nur mit einer Kopie überschreiben. – user0042

Antwort

2

nicht Arrays verwenden Sie verwenden:

std::vector<Contact> addressBook; 

statt

static Contact addressBook[CAPACITY]; 

Und glauben Sie wirklich brauchen sie statisch definieren?

Mit Vektor brauchen Sie die Variable "used" nicht. Wenn Sie wissen möchten, wie viele Kontakte, die Sie haben, müssen Sie nur

addressBook.size(); 

Jetzt schreiben, wenn Sie für einen bestimmten Kontakt suchen möchten, können Sie finden:

if(find(addressBook.begin(), addressBook.end(), my_contact) != addressBook.end()){ 
... 
} 
0

Sie haben eine Variable namens "used", also denke ich, dass Sie diese Variable verwenden möchten, um zu verfolgen, wie viele Positionen im Array gefüllt sind, dann müssen Sie diese Variable nur erhöhen, wenn Sie ein anderes Feld in Ihrem Array füllen, und dann können Sie überprüfen, ob Der Punkt wird bereits verwendet, indem Sie etwa Folgendes tun: if (i> = used) {} ​​ Sie müssen sich nur daran erinnern, dass das Array bei Index 0 beginnt und wenn das yo gefüllt ist Die verwendete Variable liegt bei 1, also ist sie immer um eins höher als der letzte gefüllte Index.

0

Wenn Sie den Speicherbedarf Ihrer Anwendung reduzieren möchten Sie call_once verwenden können, um Vektor, auf dem ersten Mal, wenn Sie fügen Sie ein Element an die Adresse Buch

std::vector<Contact> addressBook; 

void AddContact(string Contact) { 
    std::call_once(onceFlag, [this] { this->addressBook.reserve(1000); cout << "size Reserved" << endl; }); 
    addressBook.push_back(Contact); 
} 

„Größe Reserviert“ wird nur intialize erscheinen einmal

Verwandte Themen