Having trouble with array sizw

Sep 25, 2015 at 4:24pm
Sorry for the second thread, but for some reason, when this program runs, it doesn't give the expected output.

In theory, by using the size function, the [] operator, and the constructor, the program should print the number 13, 5 times. For some reason the screen is blank when it is running.

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>

using namespace std;

template <class T>
class Vector{
    public:
        T arr[];

        Vector();
        Vector(int n, const T& val);
        Vector(const Vector<T>& v);

        virtual ~Vector();

        unsigned int size();
        void push_back(const T&);
        void pop_back();
        T& at(int);

        T& operator[](int n){
            return arr[n];
        }

    protected:
};

//Constructors
template <class T>
Vector<T>::Vector(){
    arr [0];
}

template <class T>
Vector<T>::Vector(int n, const T& val){
    T arr[n];
    int index = 0;
    while(arr.begin() != arr.end()){
        arr[index] = val;
    }
}

template <class T>
Vector<T>::Vector(const Vector<T>& v){
    arr = v;
}

//Deconstructor
template <class T>
Vector<T>::~Vector(){
    delete arr;
}

//Methods
//Size will get the size of the array
template <class T>
unsigned int Vector<T>::size(){
    int total = 0;
    while(arr.begin() != arr.end()){
        total++;
    }
return total;
}

//Push_back will add a value to the end
template <class T>
void Vector<T>::push_back(const T& elt){
    int nelts, index = 0;
    //Acquire size
    nelts = arr.size();
    //We need a temp copy
    T temp[nelts];
    while(arr){
    temp[index] = arr[index];
    index++;
    }
    //We change the array
    index = 0;
    T *arr = new Vector[nelts+1];
    while(temp){
        arr[index] = temp[index];
        index++;
    }
    //Add the last value
    arr[index] = elt;
}

//Pop_back will delete the last value
template <class T>
void Vector<T>::pop_back(){
    int nelts, index = 0;
    //Acquire size
    nelts = arr.size();
    //We need a temp copy
    T temp[nelts];
    while(arr){
        temp[index] = arr[index];
        index++;
    }
    //We change the array
    index = 0;
    T *arr = new Vector[nelts-1];
    while(arr){
        arr[index] = temp[index];
        index++;
    }
}

//Pop_back will delete the last value
template <class T>
T& Vector<T>::at(int pos){
    return &arr[pos];
}


int main(){
    Vector<int> testvec(5,13);
    for(int i = 0; i < testvec.size();i++)
        cout << testvec[i] << endl;
    return 0;
}


any help is welcomed
Topic archived. No new replies allowed.