Constructor not constructing!
Hello again! following this little blurb are two things, my main.cpp which calls my class bag and my bag.h where bags and iterators for those bags are defined. When I construct a bag in main.cpp my program crashes and I can't figure out why. Any help would be appreciated!
main.cpp
Code:
//Program Name: main.cpp
#include<iostream>
#include<fstream>
#include<string>
#include<math.h>
#include<time.h>
using namespace std;
#include "bag.h"
void main() {
string file1;
ifstream fin;
cout<<"Where is your list of chores?\n";
cin>>file1;
fin.open(file1.c_str());
bag<string> chores;
char item[256];
int x=0;
while((cin.getline(item,256,'\n'))!=NULL)
{
chores.insert(item);
}
bag<string>::Iterator itr;
cout<<"Here is your list of chores:\n";
for(itr=chores.begin();!(itr==chores.end());++itr)
cout<<*itr;
itr=chores.begin();
string chore;
char task;
while(!(chores.begin()==chores.end()))
{
srand(unsigned(time(NULL)));
int x=(rand()%chores.size()); //random number 1-6
cout<<"Would you like to "<<*itr<<"? (y/n)\n";
cin>>task;
if(task=='Y'||task=='y')
{
cout<<"When you're finished come back and hit enter.";
cin.get();
cout<<"Good! You've finished your task!\n";
chore=*itr;
chores.remove(chore);
}
else
{
cout<<"Okay, we'll do that later.\n";
}
}
cout<<"You're completed all of your chores, go have fun!\n";
}
bag.h
Code:
//Programe Name: Bag.h
template<class item>
class BIter
{
public:
BIter(){}
BIter(item* p){}
item& operator*() {return *ptr;}
BIter& operator++(){ptr++; return *this; }
bool operator==(const BIter& b)
{
return ptr==b.ptr;
}
private:
item *ptr;
};
template<class type>//allows more variability than typedef
class bag
{
public:
bag(int init_cap=30);
bag(bag& b); //copy constructor
void insert(type);
void remove(type);
void operator=(bag b);
//Post: The bag has a deep copy of bag b
int size(){return used;}
~bag();//destructor
typedef BIter<type> Iterator;
Iterator begin()
{
Iterator x=pBegin;
return x;
}
Iterator end()
{
Iterator x=pEnd;
return x;
}
private:
int used;//a member to record the number of items in bag
type *pData;//a pointer to point to the dynamic array
int capacity;//a member to record the capacity of a bag
type *pBegin;
type *pEnd;
};
template<class type>
bag<type>::bag(int init_cap)
{
used=0;//an empty bag
pData=new type[init_cap];//a dynamic array is created
//and assigned to the pointer pData
capacity=init_cap;
*pBegin=pData[0];
*pEnd=pData[used];
}
template<class type>
void bag<type>::insert(type x)
{
if(used==capacity)
return;
pData[used]=x;
used++;
*pEnd=pData[used];
}
template<class type>
void bag<type>::remove(type t)
{
for(int i=0;i<used;i++)
if(pData[i]==t)
{
pData[i]=pData[used-1];
used--;
return;
}
}
template<class type>
void bag<type>::operator=(bag b)
{
if(capacity!=b.capacity)
return;
for(int i=0;i<b.used;i++)
pData[i]=b.pData[i];
used=b.used;
}
template<class type>
bag<type>::bag(bag& b)
{
capacity=b.capacity;
pData=new type[capacity];
for(int i=0;i<b.used;i++)
pData[i]=b.pData[i];
used=b.used;
}
template<class type>
bag<type>::~bag()
{
delete [] pData;
}