0

I have to write an ArrayDictionary.cpp file that follows the ArrayDictionary.h file listed below. The purpose of the .h file is an array-based implementation of the ADT dictionary that organizes its data items in sorted search-key order. Search keys in the dictionary are unique.

The code includes ArrayDictionary.h and ArrayDictionary.cpp. The ArrayDictionary.h file also includes other .h files. Please let me know if you need them to help me.

I'm having trouble with variables that cannot be identify in the ArrayDicionary.cpp file. The variables are items and entryCount that are having errors.

//ArrayDictionary.h
#pragma once

  #ifndef _ARRAY_DICTIONARY
 #define _ARRAY_DICTIONARY

 #include "DictionaryInterface.h"
 #include "Entry.h"
 #include "NotFoundException.h"
#include "PrecondViolatedExcept.h"

template < class KeyType, class ValueType>
class ArrayDictionary : public DictionaryInterface < KeyType, ValueType>
{
private:
static const int DEFAULT_CAPACITY = 21; // Small capacity to test for
                                           // a full dictionary

Entry<KeyType, ValueType>* entries; // Array of dictionary entries
int entryCount;                    // Maximum capacity of the dictionary
void destroyDictionary();
int findEntryIndex(int firstIndex, int lastIndex, const KeyType & searchKey) const;
int maxEntries;

public:
ArrayDictionary();
ArrayDictionary(int maxNumberOfEntries);
ArrayDictionary(const ArrayDictionary<KeyType, ValueType>&dictionary);

virtual ~ArrayDictionary();

bool isEmpty() const;
int getNumberOfEntries() const;
bool add(const KeyType& searchKey, const ValueType& newValue) throw (PrecondViolatedExcep);
bool remove(const KeyType& searchKey);
void clear();
ValueType getValue(const KeyType& searchKey) const throw (NotFoundException);
bool contains(const KeyType& searchKey) const;
    
/** Traverses the items in this dictionary in sorted search-key order
 and calls a given client function once for each item. */
void traverse(void visit(ValueType&)) const;
}; // end ArrayDictionary

#include "ArrayDictionary.cpp"
#endif

    

    #include <iostream>
#include "ArrayDictionary.h"
#include "PrecondViolatedExcept.h"

template < class KeyType, class ValueType>
void ArrayDictionary<KeyType, ValueType>::destroyDictionary()
{
    delete[] items;

    items = new Entry[maxEntries];

    entryCount = 0;
}

template < class KeyType, class ValueType>
inline int findEntryIndex(int firstIndex, int lastIndex, const KeyType& searchKey)
{
    int IndexMiddle = firstIndex + (lastIndex - firstIndex) / 2;

    if (firstIndex > lastIndex)

        return -1;

    else if (searchKey == items[IndexMiddle].getKey())

        return IndexMiddle;

    else if (searchKey < items[IndexMiddle].getKey())

        return findEntryIndex(firstIndex, IndexMiddle - 1, searchKey);

    else

        return findEntryIndex(IndexMiddle + 1, lastIndex, searchKey);

}

template < class KeyType, class ValueType>
inline ArrayDictionary<KeyType, ValueType>::ArrayDictionary() : entryCount(0), maxEntries(DEFAULT_CAPACITY)
{
    items = new Entry[DEFAULT_CAPACITY];
}

template < class KeyType, class ValueType>
inline ArrayDictionary<KeyType,ValueType>::ArrayDictionary(int maxNumberOfEntries) :
    entryCount(0), maxEntries(maxNumberOfEntries)
{
    items = new Entry[maxNumberOfEntries];
}


template < class KeyType, class ValueType>
inline ArrayDictionary<KeyType,ValueType>::ArrayDictionary(const ArrayDictionary& dictionary) :

    entryCount(dictionary.itemCount), maxEntries(dictionary.maxEntries)
{
    items = new Entry[dictionary.maxEntries];

    for (int index = 0; index < dictionary.entryCount; index++)
    {
        items[index] = dictionary.items[index];
    }
}

template < class KeyType, class ValueType>

inline ArrayDictionary<KeyType, ValueType>::~ArrayDictionary()
{
    destroyDictionary();
}

template < class KeyType, class ValueType>
inline bool isEmpty()
{
    return (entryCount == 0);

}

template < class KeyType, class ValueType>
inline int getNumberOfItems()
{
    return entryCount;
}

template < class KeyType, class ValueType>
inline void ArrayDictionary<KeyType, ValueType>:: clear()
{
    destroyDictionary();
}


template < class KeyType, class ValueType>
inline bool ArrayDictionary<KeyType, ValueType>::add(const KeyType& searchKey, const ValueType& newValue) throw (PrecondViolatedExcep)
{
    bool ableToInsert = (entryCount < maxEntries);

    if (ableToInsert)
    {
        // Make room for new entry by shifting all entries at
        // positions >= newPosition toward the end of the array
        // (no shift if newPosition == itemCount + 1). Performing
        // a binary search doesn’t help here, because we need to
        // shift the entries while looking for the insertion location.

        int index = entryCount;

        // Short-circuit evaluation is important

        while ((index > 0) && (searchKey < items[index - 1].getKey()))
        {
            items[index] = items[index - 1];
            index--;
        }  // end while

        if (searchKey != items[index - 1].getKey())
        {
            items[index] = Entry<KeyType, ValueType>(searchKey, newValue);
            entryCount++;

        }
        else
        {
            auto message = "Attempt to add entry whose search key exits in dictionary.";
            throw (PrecondViolatedExcep(message);
        }

        return ableToInsert;
    }  // end add

}

template < class KeyType, class ValueType>
inline bool ArrayDictionary<KeyType, ValueType>:: remove(const const KeyType& itemKey)
{
    int currentIndex = findEntryIndex(0, itemCount - 1, itemKey);

    bool deletable = !isEmpty() && (currentIndex >= 0);

    if (deletable)
    {
        while (currentIndex < entryCount - 1)
        {
            items[currentIndex] = items[currentIndex + 1];

            currentIndex++;
        }
        itemCount--;
    }
    return deletable;
}

template < class KeyType, class ValueType>
inline ValueType getValue(const KeyType& searchKey) throw(NotFoundException)
{
    int currentIndex = findEntryIndex(0, itemCount - 1, searchKey);

    if (currentIndex < 0)
        throw NotFoundException("nnItemis not in the Dictionary!nn");

    return items[currentIndex].getItem();
}

template < class KeyType, class ValueType>
inline bool contains(const KeyType& searchKey)
{
    return (findEntryIndex(0, entryCount - 1, itemKey) >= 0)
}

template < class KeyType, class ValueType>
inline void traverse(void visit(ValueType&))
{
    for (int itr = 0; itr < entryCount; itr++)
    {
        ValueType currentItem = items[itr].getItem();

        visit(currentItem);
    }
}
  • 5
    `ArrayDictionary.h` has at the end, `#include "ArrayDictionary.cpp"`. `ArrayDictionary.cpp` at the start has `#include "ArrayDictionary.h`. That's not going to end well. – NathanOliver Jun 30 '21 at 18:54
  • Since you have `#include "ArrayDicitionary.cpp"` at the end of `ArrayDictionary.h`, remove `#include "ArrayDictionary.h"` from `ArrayDictionary.cpp`. – R Sahu Jun 30 '21 at 18:57
  • Also, many of the functions in the `.cpp` are missing the `ArrayDictionary::` qualifier (`destroyDictionary`, `findEntryIndex`, etc). For instance, change `template < class KeyType, class ValueType> inline void destroyDictionary()` to `template < class KeyType, class ValueType> void ArrayDictionary::destroyDictionary()`, etc. In some other functions, `ArrayDictionary` should be `ArrayDictionary` instead. – Remy Lebeau Jun 30 '21 at 19:00
  • @Lacy9265 What are the actual errors? It means you are doing something else wrong. – Remy Lebeau Jun 30 '21 at 19:02
  • 1
    Try renaming `ArrayDicitionary.cpp` to `ArrayDicitionary.tpp`. Also don't forget to fix the include at the end of `ArrayDicitionary.h` to match. – NathanOliver Jun 30 '21 at 19:02
  • @NathanOliver I would have suggested `.ipp` instead. The file name doesn't really matter to the compiler though, so this is just personal choice what to name the file. – Remy Lebeau Jun 30 '21 at 19:03
  • Well when I delete the ArrayDictionary.h from ArrayDictionary.cpp file I get 87 errors. Also, the requirement for the code is too have an ArrayDictionary.cpp and ArrayDictionary.h file. –  Jun 30 '21 at 19:08
  • Sometimes fixing one big error reveals dozens or hundreds of smaller errors that the compiler previously could not detect. Do the right thing and ignore the error count. Of course figuring out what the right thing is can often be tricky. But doing the wrong thing and hiding errors is, well, the wrong thing to do. – user4581301 Jun 30 '21 at 19:11
  • 2
    Handy reading: [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) So you're doing the right thing by making the template implementation visible from the header, but often smart build tools will see the .cpp extension and automatically build and link the .cpp file, resulting in multiple definition errors. If you have to give the instructor a cpp file, give them the cpp file. But make sure the stuff in that cpp file is not going to be repeated. – user4581301 Jun 30 '21 at 19:13
  • Side note: I see a destructor and a copy constructor, but to finish off the [Rule of Three](https://en.cppreference.com/w/cpp/language/rule_of_three) you should add an assignment operator. – user4581301 Jun 30 '21 at 19:16
  • I have taken some of suggestions and updated the ArrayDictionary.cpp file above. However, I'm still having the same issue where items and itemCount identifier not found and undeclared identifier. –  Jun 30 '21 at 19:18
  • I had a couple variables wrong in my code. However, I'm still having trouble with items and now a variable entryCount not being identifier or undeclared. Please can someone help me fix my code. –  Jun 30 '21 at 19:34
  • OT: If you are going to list the class number, please state which college or university you are attending. Different institutions have different numbering schemes. My university didn't have any undergraduate class under 500. Also, please include a summary or the class title. – Thomas Matthews Jun 30 '21 at 20:22

0 Answers0