C++ contacts handler program with own dynamic template vector class -
i working on project have university. want simple contants program, goal is, can add many names, want, furthermore many properties each names want. every proberty has header, (e.g.:"name") , value (e.g.:"jane").
i have did it, included stl vector class, ought without stl libraries, have written own. of course, isn't working now, program freezes, before can begin. in connection error, have found "a_row" class has been called twice during program have no idea why.
i apoligise copying here lot rows, have been searching problem 3 days nobody find problem, around me. here code, have simplified it, :)
the vector class:
#include <iostream> #include <string> template <class t> class vector{ t* my_array; unsigned int vector_size; public: vector():vector_size(0){} ~vector(){ delete[] my_array; } vector(const vector& rhs){ vector_size = rhs.size(); my_array = new t[vector_size]; for(unsigned int i=0;i<vector_size;i++) my_array[i] = rhs[i]; } t& operator[](unsigned int i) {return my_array[i];} const t& operator[](unsigned int i) const {return my_array[i];} vector& operator=(const vector& rhs){ if (&rhs!=this){ delete[] my_array; vector_size = rhs.size(); my_array = new t[vector_size]; for(unsigned int i=0;i<vector_size;i++) my_array[i] = rhs[i]; } return *this; } void push_back(const t& rhs){ t* tmp = new t[vector_size+1]; for(unsigned int i=0;i<vector_size;i++) tmp[i] = my_array[i]; tmp[vector_size] = rhs; delete[] my_array; my_array = tmp; vector_size++; } unsigned int size() const{return vector_size;} void erase(unsigned int x){ if (vector_size>0){ t* tmp = new t[vector_size-1]; unsigned int counter = 0; (unsigned int i=0;i<vector_size;i++){ if (i!=x) { tmp[counter++] = my_array[i]; } } delete[] my_array; my_array = tmp; vector_size--; } } }; here classes of contacts program:
class a_row{ std::string header; std::string value; public: a_row(const std::string& h="no name",const std::string& v="no "):header(h),value(v){} std::string geth() const{return header;} std::string getv() const{return value;} }; class a_name_card{ vector<a_row> properties; public: a_name_card(const std::string& f1="no name",const std::string& e1="no name"){ properties.push_back(a_row(f1,e1)); } unsigned int size()const{return properties.size();} a_row& get_a_row(int i){return properties[i];} void add(const std::string& f1="no name",const std::string& e1="no name"){ properties.push_back(a_row(f1,e1)); } }; class list_of_name_cards{ vector<a_name_card> array_of_names; public: a_name_card& operator[](int i){return array_of_names[i];} void add(const std::string& f1="no name", const std::string& e1="no name"){ array_of_names.push_back(a_name_card(f1,e1)); } unsigned int size() const{return array_of_names.size();} }; and here simple main program:
int main(){ list_of_name_cards names; names.add("name","jane"); return 0; } i appreciate kind of helps , again if you've read it.
p.s.: sorry english english not mother tongue.
you need initialize variables:
vector():vector_size(0){} what my_array?
vector():my_array(nullptr), vector_size(0){}
Comments
Post a Comment