asmcriminal
asmcriminal

Reputation: 93

Attempting to reference a deleted function.

I am having difficulty with my code. I am getting the error "AssociativeArray>::Node::Node(void)': attempting to reference a deleted function"

Here is my class

template<typename K, typename V>
class AssociativeArray
{
  public:
    AssociativeArray(int = 2);  // default constructor of a capacity of 2
    AssociativeArray(){ delete [] data; }
    AssociativeArray(const AssociativeArray<K,V>&); 
    AssociativeArray<K,V>& operator=(const AssociativeArray<K,V>&);
    V& operator[](const K&); // getter
    V operator[](const K&) const;  // setter
    queue<K> keys() const;
    int size() const {return siz;};
    bool containsKey(const K&) const;
    void deleteKey(const K&);
    void clear();

  private: 
    int cap;
    int siz;
    int values;
    void capacity(int);
    struct Node{K key; V value; bool inUse;};
    Node* data;  // data = new Node[cap]
    void ExpandNode(int,int);
};

Here is the constructor which is giving me the problem.

template<typename K, typename V>
AssociativeArray<K, V>::AssociativeArray(int cap)
{
  this->cap = cap;
  this->siz = 0;

  data = new Node[cap];  // ERRORS HERE

  for (int index = 0; index < cap; index++)
  {
    data[index].inUse = false;
  }
}

Here is the declaration in main.

AssociativeArray<string, AssociativeArray<string, bool>> seen(10);

Upvotes: 2

Views: 2562

Answers (2)

asmcriminal
asmcriminal

Reputation: 93

The issue was there was no destructor. I forgot to add the ~ in front of "AssociativeArray(){ delete [] data; }" Thus I suspect I had 2 constructors and it was calling the constructor that deleted the data array.

Upvotes: 1

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145194

The Node class' default constructor is used for each item when you create a raw array of Node.

For some reason there is no such default constructor. Presumably the Key member lacks a default constructor. However, you're not showing the code so this about reasons is just speculation.

One good way to create an array without invoking the default constructor for the item type, is to use a std::vector. Alternatively, if it is meaningful to do so then you can just define a default constructor. But using a std::vector also deals with a host of other problems, so I recommend that you do that regardless.

Upvotes: 1

Related Questions