0

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.

user3290356
  • 95
  • 1
  • 10
  • It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: **[How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver Apr 07 '17 at 19:00
  • 1
    `vector& operator=(const vector& rhs){` -- There is an issue with this function. You delete all the data before calling `new[]`. If `new[]` throws an exception, you have no way to get the data back, thus your object is corrupted. There is a *very simple* fix for all of this, namely `copy / swap`. – PaulMcKenzie Apr 07 '17 at 19:11
  • 1
    Also, why aren't you writing simple unit tests for your `vector` functions before you actually start to use it in an application that requires proper functionality of your vector class? You should have tested copying, construction, assignment, destruction, erasure, etc. **before** you start to use it in another app. Once those tests pass, *then* you use your vector class. In addition, don't call your class `vector`, as it could clash with the C++ `vector` class. Put your vector class in its own namespace if you must call it `vector`. – PaulMcKenzie Apr 07 '17 at 19:16
  • @OP See [this question and answer](http://stackoverflow.com/questions/39950635/c-linked-list-assignment-operator). Even though it discusses linked list, the same thing applies to your `vector` class. Write the assignment operator easily and correctly. – PaulMcKenzie Apr 07 '17 at 19:27

1 Answers1

3

You need to initialize your variables:

vector():vector_size(0){}

What about my_array?

vector():my_array(nullptr), vector_size(0){}
Khouri Giordano
  • 796
  • 3
  • 9
  • It worked, thanks a lot! I assume I have to use "NULL" instead of "nullptr" because I'm not using c++11? Or my I ask why? – user3290356 Apr 07 '17 at 19:21
  • Furthermore can anybody tell me why worked the class perpect when I tried it with simple int and double values? I'm curios! – user3290356 Apr 07 '17 at 19:22
  • What compiler are you using? In this day and age, you should learn with (at least) a C++ 11 compliant compiler. – PaulMcKenzie Apr 07 '17 at 19:22
  • @user3290356 -- Undefined behavior. If you don't initialize your variables, then you don't know how your app will behave. – PaulMcKenzie Apr 07 '17 at 19:23
  • I have downloded the latest codeblocks with the standard compiler. I'll learn c++11 but I just started the university in September :) Ohh, I understand, thanks a lot again! :) – user3290356 Apr 07 '17 at 19:27