I am working on a project what I have to do for the university. I want to do a simple contants program, my goal is, that I can add as many names, as I want, furthermore as many properties for each names as I want. Every proberty has a header, (e.g.:"Name") and a value (e.g.:"Jane").
I have already did it, with the included STL vector class, but I ought to do it without STL libraries, so I have written my own. Of course, it isn't working now, a program freezes, before it can begin. In connection with the error, I have found that the "a_row" class has been called twice during the program but I have no idea why.
I would like to apoligise for copying here a lot rows, but I have been searching the problem for 3 days it nobody could find the problem, around me. Here are the code, I have simplified it, for you :)
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;
for (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 are the classes of the 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 are the simple main program:
int main(){
list_of_name_cards names;
names.add("Name","Jane");
return 0;
}
I would appreciate any kind of helps and thanks again if you've read it.
p.s.: Sorry for my english but English is not my mother tongue.