samedi 19 septembre 2020

invalid pointer when using strings for generic class

So this is for my data structures class and I am struggling to understand why I am getting the error: invalid pointer: 0x00000000023ce048 ***. It only happens when I define my generic class using strings: NSequence<string> v3(10);

It should be noted that using int or even a different class worked fine. However, as soon as I write the line: NSequence<string> v3(10); I get the pointer error and I have no idea why?

UPDATE: I've found that it was actually my destructor class that was causing the error. However, I now have no idea why this would cause an error specifically with string?

template<typename T>
NSequence<T>::~NSequence()
{
     delete items;
}

Here is the definition of my class:

#include <algorithm>
#include <iostream>
#include <utility>

/* XXX: Implement all member functions for NSequence in NSequence.hpp */

template <typename T>
class NSequence
{
  public:
    explicit NSequence( int initSize = 0 ); 
      
  private:
    int numOfItems;
    int totalCapacity;
    T * items;
};

#include "NSequence.hpp"  // do not change this line 

#endif

Here is my constructor function:

template<typename T>
NSequence<T>::NSequence(int initSize)
{
        if(initSize==0)
                initSize=1;

        numOfItems = initSize;
        totalCapacity = initSize;
        items = new T[totalCapacity];

}

Any advice would be greatly appreciated!

Aucun commentaire:

Enregistrer un commentaire