Error in get function

I was trying to create my own vector class for basic operations push_back, pop_back and at. i was able to do pushback and popback successfully but not at function, can anyone tell me what wrong with my code?


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
#ifndef NVECTOR_H
#define NVECTOR_H
#include <cstdlib>

using namespace std;

const unsigned DEFAULT_SIZE = 5;

template <class Temp>
class nVector
{
public:
    nVector(){create(DEFAULT_SIZE);};
    nVector(unsigned i){create(i);};

    unsigned size();
    void push_back(Temp _Member_M);
    void pop_back(Temp _Member_M);
    void at(Temp _Member_M);
    Temp *begin();
    Temp *end();
    void clear();

    Temp operator[](int i);
private:
    Temp *reSize(unsigned newSize);
    bool create(unsigned i);

    int vector_Dimension;
    unsigned new_Dimension;
    Temp *array;
};

template <class Temp>
unsigned nVector<Temp>::size()
{
    return vector_Dimension;
}

template <class Temp>
unsigned nVector<Temp>::at(Temp element)
{ return (*this)[Temp]; }

template <class Temp>
void nVector<Temp>::pop_back(Temp element) {
    --vector_Dimension;
}

template <class Temp>
void nVector<Temp>::push_back(Temp element)
{
    if (new_Dimension <= (float)vector_Dimension)
        reSize((unsigned)((vector_Dimension * 2) + 1));
    *end() = element;
    vector_Dimension++;
}

template <class Temp>
Temp *nVector<Temp>::begin()
{
    return array;
}

template <class Temp>
Temp *nVector<Temp>::end()
{
    return array + vector_Dimension;
}

template <class Temp>
void nVector<Temp>::clear()
{
    delete [] array;
    array == NULL;
    new_Dimension = 0;
    vector_Dimension = 0;
}

template <class Temp>
Temp nVector<Temp>::operator[](int i)
{
    return *(array + i);
}

template <class Temp>
Temp *nVector<Temp>::reSize(unsigned Temp_Dimension)
{
    Temp *newArray = new Temp[(unsigned)Temp_Dimension];
    Temp *fromPtr = begin();
    Temp *toPtr = newArray;
    while(fromPtr != end())
    {
        *(toPtr++) = *(fromPtr++);
    }

    delete [] array;
    array = newArray;
    new_Dimension = Temp_Dimension;

}

template <class Temp>
bool nVector<Temp>::create(unsigned i)
{
    array = new Temp[i];
    if(array == NULL)
        return false;

    new_Dimension = i;
    vector_Dimension = 0;
    return true;
}

#endif 
1
2
3
template <class Temp>
unsigned nVector<Temp>::at(Temp element)
{ return (*this)[Temp]; }


You never use element argument in function body.
Instead you are trying to return an array with size defined by the element type (Temp);

Consider Temp being int. Function template becomes:
1
2
unsigned nVector<int>::at(int element)
{ return (*this)[int]; }


doesn't make much sense right?
I guess you meant
1
2
unsigned nVector<int>::at(int element)
{ return (*this)[element]; }
Now i removed all the templates form that code, and my new class looks like:

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
class nVector
{
public:
    nVector()
    {
    	create(DEFAULT_SIZE);
    };

    nVector(int i)
    {
    	create(i);
    };

    int size();
    void push_back(int);
    void pop_back(int);
    void at(int);
    int *begin();
    int *end();
    void clear();

    int operator[](int i);
private:
    int *reSize(int newSize);
    bool create(int i);

    int vector_Dimension;
    int new_Dimension;
    int *array;
};


and at function looks like:
1
2
void nVector::at(int)
 { return array; }


i do have 2 problems in this code, i am not able to use at function again. And i cannot print the vector, please see the print function below:
1
2
3
4
5
6
7
8
void print(nVector & a)
 	{
	    cout<<"Elements inside the vectors are:"<<endl;
 		for(int i=0; i<a.size(); i++)
 			cout<< a[i] << " ";
 		    cout << endl;
 		    cout << "----------------"<<endl;
 	}


It is printing only 0s. Is there anything wrong inside?
You use operator[] and not the at function in that code.
at() is the same like the operator[], So: Temp at(int i);
The body is also the same

EDIT: Temp at(int i) const; is better so you can apply it to a const nVector & a as well
Last edited on
Topic archived. No new replies allowed.