c++ using vector of employee pointers -


i have created vector class template , have done employee classes of hourly , salaried. want use vector of employee pointers instead of array of employee pointers, i'm trying when run breaks while have no error listed. also, have used at function like( payroll.at(i)->writefile(out); ) access element, don't know what's wrong. suggestion? thanks

here code: myvector class template:

#include <iostream> #include <string> #include <cassert> #include <algorithm>    const int capacity = 4;   template <class t>   class  myvector {   public:      myvector();     myvector( int size);     myvector( int size, const t & initial);     myvector(const myvector<t> & v);           ~myvector();      int capacity() const;     int size() const;     void push_back(const t & value);      //t & operator[](unsigned int index);       myvector<t> & operator=(const myvector<t> &);     void clear();     t at(int i);      friend ostream& operator<<(ostream &out, const myvector<t>& );     private:     int applied;     int my_size;     int my_capacity;     t * buffer;     t * daarray; };  template<class t> myvector<t>::myvector() {   my_capacity = 0;   my_size = 0;   buffer = 0;   applied = 0; }  template<class t> myvector<t>::myvector(const myvector<t> & v) {   my_size = v.my_size;   my_capacity = v.my_capacity;   buffer = new t[my_size];     ( int = 0; < my_size; i++)     buffer[i] = v.buffer[i];   }  template<class t> myvector<t>::myvector(int size) {   my_capacity = size;   my_size = size;   buffer = new t[size]; }  template<class t> myvector<t>::myvector( int size, const t & initial) {   my_size = size;   my_capacity = size;   buffer = new t [size];   (unsigned int = 0; < size; i++)     buffer[i] = initial;   //t(); }  template<class t> myvector<t> & myvector<t>::operator = (const myvector<t> & v) {   delete[ ] buffer;   my_size = v.my_size;   my_capacity = v.my_capacity;   buffer = new t [my_size];   (int = 0; < my_size; i++)     buffer[i] = v.buffer[i];   return *this; }  template<class t> void myvector<t>::push_back(const t & i) {   if (my_capacity == 0)   {     my_capacity = 1;     my_size = 1;     applied= 0;     buffer = new t[1];     buffer[0] = i;   }   else   {     if (applied+1 == my_capacity)     {       int newcapacity = my_capacity * capacity;       daarray = new t[newcapacity];       (int = 0; < my_size; i++)       {         daarray[i] = buffer[i];         }       my_capacity = newcapacity;           delete buffer;       my_size++;       applied++;       buffer[applied] = i;     }     else     {       if (my_size == applied + 1)         my_size++;       applied++;       buffer[applied] = i;     }   } }  template<class t> int myvector<t>::size()const// {   return my_size; }  template<class t> int myvector<t>::capacity()const {   return my_capacity; }  template<class t> myvector<t>::~myvector() {   delete[ ] buffer; }  template <class t> void myvector<t>::clear() {   my_capacity = 0;   my_size = 0;   buffer = 0; }  template <class t> t myvector<t>::at(int i) {   if (i < 0 || > my_size -1)   {     string error = "index undefined";     throw error;   }   return buffer[i]; }  template <class t> ostream& operator<<(ostream &out, const myvector<t>& v) {   (unsigned = 0; < v.size(); i++)   {     out << v[i] << " ";   }   return out; } 

main

int main() {   myvector< employee*> payroll;   payroll.push_back(new hourly ("h. potter", "privet drive", "201-9090", 40, 12.00));   payroll.push_back(new salaried ( "a. dumbledore", "hogewarts", "803-1230", 1200));    ofstream out;   out.open(file);    if (out.fail()) {     cout<<" not open file"<<endl;     system("pause");    }    (int = 0; < size; i++) {     payroll.at(i)->writefile(out);   }   out.close( ); } 

you have bug in push_back method. need this

if (applied+1 == my_capacity) {     int newcapacity = my_capacity * capacity;     daarray = new t[newcapacity];     (int = 0; < my_size; i++)     {         daarray[i] = buffer[i];       }     my_capacity = newcapacity;       delete buffer;     buffer = daarray; // new line here     my_size++;     applied++;     buffer[applied] = i; } 

see i've put comment // new line here


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -