Hello everyone, and thanks for taking your time to help me.
I have more than one question/issue/doubt, but the main one I think is the one regarding compiling, so that's why I created the thread in this section. Hopefully you can help me either way, and forgive me if I was wrong.
Anyway, I have an assignment in which I have to create my own vector class using a template. It's for the most part complete, but of course, I'm here for a reason. Here's what I wrote:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
#ifndef VECTOR_H
#define VECTOR_H
#include <iostream>
using namespace std;
template <class T>
class Vector
{
public:
Vector()
{
total_size = 2;
data = new T[total_size];
current_size = 0;
}
Vector& operator=(const Vector &other_vector)
{
if ( this != &other_vector)
{
delete [] data;
total_size = other_vector.size;
current_size = other_vector.current_size;
data = new T[total_size];
for (int i = 0; i < current_size; ++i)
{
data[i] = other_vector.data[i];
}
}
return *this;
}
Vector& operator[](const int& i) // work on this
{
return data[i];
}
Vector(Vector& other_vector)
{
this = other_vector;
}
~Vector()
{
total_size = 2;
current_size = 0;
delete [] data;
}
void push_back(const T &e)
{
current_size += 1;
data.increment_size();
data[current_size] = e;
}
void pop_back()
{
current_size -= 1;
}
void increment_size()
{
if (current_size == total_size)
{
size += 4;
T *temp = new T[total_size];
for (int i = 0; i < current_size; ++i)
{
temp[i] = data[i];
}
delete [] data;
data = new T[total_size];
for (int i = 0; i < current_size; ++i)
{
data[i] = temp[i];
}
delete [] temp;
}
return 0;
}
int size() const
{
return current_size;
}
private:
int total_size;
int current_size;
T *data;
};
#endif
| |
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <iostream>
#include "Vector.h"
using namespace std;
int main()
{
Vector<int> v1;
cout << v1.size();
return 0;
}
| |
I wanted to test my class progressively, so the first thing I wanted to test was the size function.
So I have my Vector class basically done. I'm assuming there are problems with the destructor, because if I don't comment it out, I get so many errors I can't even see the first one, I'm assuming a segmentation fault. If I comment the destructor out and compile, it runs.
I use the command g++ -o Vector.h main.cpp, but here's the problem. It doesn't return an a.out. I'm sure it's not something wrong with anything, I'm sure it's a lack of knowledge on my part, but not matter how many google searches I do, I can't find the reason why this is happening. Since it doesn't gives out an a.out, I can't test it, therefore I can't see if there are any more problems than syntax errors.
So basically that's my problem, I don't know how to test this class. If you see any glaring problem in my class, feel free to point them out. I'm pretty sure there's something wrong with both operators, but again, can't test for them. Thank you very much for the help in advance.