2

I'm trying to create an empty array of objects of the class Contact. Starting with an empty array, I want to create a function in AddrBook.cpp to add objects of Contact to the object array, called addressBook.

Am I initializing the array properly in AddrBook.h?

How do I check to see if an object of Contact exists at a specific index?


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
2
  • 1
    Arrays have a fixed size. You cannot have an empty array. It always has exactly CAPACITY number of elements. If you require the size to change use an std::vector. Commented Sep 17, 2017 at 12:16
  • There already exists an object at position i. The only thing you can do is to overwrite it with a copy. Commented Sep 17, 2017 at 12:20

3 Answers 3

2

Don't use arrays, use:

 std::vector<Contact> addressBook;

instead of

 static Contact addressBook[CAPACITY];

And do you really need to define it static?

With vector you don't need the variable "used". If you want to know how many contacts you have, you only need to write

 addressBook.size();

Now, if you want to look for a specific contact, you can use find:

 if(find(addressBook.begin(), addressBook.end(), my_contact) != addressBook.end()){
...
}
Sign up to request clarification or add additional context in comments.

Comments

0

You have a variable called 'used', so I guess you want to use this variable to keep track of how many positions in the array are filled, then you just have to increment that variable when you fill another space in your array and then you can check if the spot is already used,b y doing something like this: if(i>=used){} You just have to remember that the array start at index 0 and when that is filled your variable used is at 1, so it always is one higher than the last filled index.

Comments

0

If you want to reduce the memory footprint of your app you can use call_once to intialize your vector, on the first time you add an element to the adress book

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

"size Reserved" will only appear once

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.