Tuesday, October 1, 2013

Error with copy constructor

Error with copy constructor

My program keeps dyeing when trying to allocate new memory for my array.
By calling the add word function. Any help would be great!
Full code download @ http://www.johnsoncomputertechnologies.org/pgm2demo.zip
#include <iomanip> //Standard IO Manipulators Library
#include <algorithm>
#include <iostream> //Standard input/output library
#include <string> //Standard string Library
#include <cassert> //Error handeling Library
#include "pgm2.h"
using namespace std;
dictionary::dictionary()
{
wordptr = new value_type[DICTONARY_CAPACITY];
deffptr = new value_type[DICTONARY_CAPACITY];
used = 0;
}
dictionary::dictionary(dictionary& other)
{
value_type *wordptrs;
value_type *deffptrs;
wordptrs = new value_type[other.capacity];
deffptrs = new value_type[other.capacity];
used = other.used;
copy(other.wordptr, other.wordptr + used, wordptrs);
copy(other.deffptr, other.deffptr + used, deffptrs);
}
dictionary::~dictionary()
{
delete [] wordptr;
delete [] deffptr;
}
//Functions
void dictionary::reserve(size_type new_capacity)
{
value_type *larger_word;
value_type *larger_deff;
if(new_capacity == capacity)
return; //There allocated memory is already the correct size
if(new_capacity < used)
new_capacity = used;
larger_word = new value_type[new_capacity];
larger_deff = new value_type[new_capacity];
copy(wordptr, wordptr + used, larger_word);
copy(deffptr, deffptr + used, larger_deff);
delete [] wordptr;
delete [] deffptr;
wordptr = larger_word;
deffptr = larger_deff;
capacity = new_capacity;
}
void dictionary::addword(const value_type& word, const value_type& deff)
{
if(used == capacity)
reserve(used+1);
wordptr[used] = word;
deffptr[used] = deff;
++used;
}
pgm2.h
#include <iomanip> //Standard IO Manipulators Library
#include <algorithm>
#include <iostream> //Standard input/output library
#include <string> //Standard string Library
#include <cassert> //Error handeling Library
using namespace std;
class dictionary
{
public:
typedef string value_type;
typedef std::size_t size_type;
static const size_type DICTONARY_CAPACITY = 25;
//Default Constructor
dictionary();
dictionary(dictionary& other);
//Destructor
~dictionary();
//Functions
void reserve(size_type new_capacity);
void addword(const value_type& word, const value_type& deff);
void startPat (value_type patt);
void endPat(value_type patt);
void containsPat(value_type patt);
void print();
private:
value_type *wordptr;
value_type *deffptr;
size_type used;
size_type capacity;
};

No comments:

Post a Comment