Adding a template to custom made vector class(College Assignment)

Basically we're taking our custom int vector class and making it into an adaptable vector class. However I'm running into these particular errors. Here's my code first:
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// File Prologue

#include <iostream>
#include <string>
#pragma once

using namespace std;
template<class T>
class MyVector
{
// Note that you program should not have any memory leaks.
private:
		T vSize;
		T vCapacity;
		T* vectorData;
		// overloaded << operator - a nonmember
		// designate as a friend so that it can utilize the private data members of the MyVector class
		friend ostream& operator<< (ostream&, const MyVector<T>&);

public:
	// MyVector(Default)
	// Purpose: A default constructor that creates an vector with a default capacity of 2  
	// Parameters: None
	// Returns: None
		MyVector<T>(void);
	
	// MyVector(Parameterized)
	// Purpose: A parameterized constructor that creates a vector of capacity n
	// Parameters: integer value of n for capacity
	// Returns: None
		MyVector<T>(const T&);
	
	// MyVector(Copy)
	// Purpose: A parameterized constructor that takes an object(designated as "b") and copies it to another object
	// Parameters: Object vector designated as "b"; Must be a constant
	// Returns: Copy of object "b"
		MyVector<T>(const MyVector<T>&);

	// MyVector(Destructor)	
	// Purpose: A destructor that deletes any dynamically allocated storage
	// Parameters: None
	// Returns: None
		~MyVector<T>(void);
	
	// size()
	// Purpose: A function, size( ), that returns the size of your vector.
	// Parameters: None
	// Returns: Size of the vector
		T size() const;
 
	// capacity()
	// Purpose: A function, capacity( ), that returns the capacity of the vector. 
	// Parameters: None
	// Returns: Capacity of Vector
		T capacity() const ;

	// clear()
	// Purpose: A function, clear( ), that deletes all of the elements from the vector and resets its size and capacity to zero. 
	// Parameters: None
	// Returns: None
		void clear();
	
	// push_back()
	// Purpose: A function push_back(int n) that adds the integer value n to the end of the vector.
	// Parameters: None
	// Returns: None
		void push_back(T&);

	// at(int n)
	// Purpose: locate the value of the element at position i in the vector. 
	// Parameters: If the index i is greater than the size( ) of the vector, this function should throw an exception. 
	// Returns: returns the value of the element at position i in the vector. 
		T at(T&) const;
	 
	// If the vector is not large enough to hold this additional value, you must make the vector grow. 
	// Your grow algorithm should double the current capacity of the vector. 
	//	Don't forget to consider the case where the initial capacity of the vector is zero.
		void allocNew();

	// Overload Assignment Operator()
	// Purpose: Overload the assignment operator
	// Parameters: const object that is designated as "b"
	// Returns: ?
		MyVector<T>& MyVector::operator=(const MyVector<T>&);	
};

// MyVector Class Code

// Default Constructor
template<class T>
MyVector<T>::MyVector(void)
{
	vCapacity = 2;
	vectorData = new T [vCapacity];
	vSize = 0;
}

// Parameterized Constructor
template<class T>
MyVector<T>::MyVector(const T& n)
{
	vCapacity = n;
	vectorData = new <T> [vCapacity];
	vSize = 0;
}

// Destructor
template<class T>
MyVector<T>::~MyVector(void)
{
	 if ( vectorData != nullptr)
    {
        delete [ ] vectorData;
        vectorData = nullptr;
    }
}

// MyVector Copy Constructor
template<class T>
MyVector<T>::MyVector(const MyVector<T>& b)
{
		vCapacity = b.vCapacity;
		vSize = b.vSize;
		delete [] vectorData;
		vectorData = new T [vSize];
		for(int i = 0; i < b.vSize;i++)
		{
			vectorData[i] = b.vectorData[i];
		}
}

// Operator Overload
template<class T>
MyVector<T>::MyVector& operator=(const MyVector<T>& b)
{
	// Test for self-copy
	if(this != &b)
	{
		// If not a self-copy, implement the following
		vSize = b.vSize;
		vCapacity = b.vCapacity;
		delete [] vectorData;
		vectorData = new <T> [vSize];
		for(int i = 0; i < b.vSize;i++)
		{
			vectorData[i] = b.vectorData[i];
		}
	}
	return *this;
}

// Size Function
template<class T>
T MyVector<T>::size() const
{
	return vSize;
}

// Push_Back Function
template<class T>
void MyVector<T>::push_back(T& n)
{
	// Allocate new memory space if the size of the vector is greater than the capacity of the vector
	if(vSize+1 > vCapacity)
	{
		allocNew();
	}
	vectorData[vSize] = n;
	vSize++;
}

// Capacity Function
template<class T>
T MyVector<T>::capacity() const
{
	return vCapacity;
}

// Clear Function
template<class T>
void MyVector<T>::clear()
{
	delete []vectorData;
	// If vectorData is not NULL then reset size and capacity to respective values of 0 and 2. Delete []vectorData and set vectorData to new array
	vSize = 0;
	vCapacity = 2;
	vectorData = new <T> [vCapacity];
}

// At Function
template<class T>
T MyVector<T>::at(T& n) const
{
	if(n < vSize)
	{
		return vectorData[n];
	}
	// Throw an exception if n is greater than vector size
	throw 10;
}

// allocNew Function
template<class T>
void MyVector<T>::allocNew()
{
	// Increase vector capacity by doubling the size of the vector by 2
	vCapacity = vSize * 2;

	// Declare new array
	T* temp = new T[vCapacity];

	// Copy data from old array into new array
	for(int i = 0; i < vSize; i++)
	{
		temp[i] = vectorData[i];
	}
	// delete old array and have new array take the place of old array
	delete [] vectorData;
	vectorData = temp;
}
template<class T>
ostream& operator<<(ostream& out, const MyVector<T>& b)
{
	for(int i = 0; i <(b.vSize); i++)
	{
		out << b.at(i) << " ";
	}
	return out;
}


and here are the errors/warnings that visual studio 2010 is giving me:

error C1903: unable to recover from previous error(s); stopping compilation myvector.h 144
error C2143: syntax error : missing ';' before '&' myvector.h 144
error C2350: 'MyVector<T>::{ctor}' is not a static member myvector.h 144
IntelliSense: initial value of reference to non-const must be an lvalue driver.cpp 42
warning C4346: 'MyVector<T>::{ctor}' : dependent name is not a type myvector.h 144


Can someone help me make sense of these errors please? Thanks in advance.
Here's the driver for reference and it's been modified for this template assignment:
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
// File Prologue

// --------------------------------------------------------
// DO NOT CHANGE ANY CODE PAST THIS POINT
//---------------------------------------------------------
#include <iostream>
#include "MyVector.h"     
using namespace std;

// the printV function
// used to test the copy constructor
// parameter: a MyVector object
template<class T>
void printV(MyVector<int>);

int main( )
{
	cout << "\nCreating a vector Sam of size 4.";
	MyVector<int> sam( 4 );

	cout << "\nPush 12 values into the vector.";
	for (int i = 0; i < 12; i++)
		sam.push_back(i);

	cout << "\nHere is sam: ";
	cout << sam;
	cout << "\n---------------\n";

	cout << "\nCreating a vector Joe of size 4.";
	MyVector<int> joe( 4 );
	cout << "\nPush 6 values into the vector.";
	for (int i = 0; i < 6; i++)
		joe.push_back(i * 3);

	cout << "\nHere is joe: ";
	cout << joe;
	cout << "\n---------------\n";
	
	cout << "\nTest the overloaded assignment operator \"joe = sam\": ";
	joe = sam;

	cout << "\nHere is sam: ";
	cout << sam;
	cout << "\n---------------\n";

	cout << "\nHere is joe: ";
	cout << joe;
	cout << "\n---------------\n";

	// pass a copy of sam by value
	printV<int>(sam);
	

	cout << endl;
	system("PAUSE");
	return 0;
}
void printV(MyVector<int> v)
{
	cout << "\n--------------------\n";
	cout << "Printing a copy of a vector\n";
	cout << v;
}
Making liberal use of Google (since my class hasn't even started making linked lists yet), you need to fix lines 18, 67, 103, 134, 143, 161, 187, and maybe 84 of your header file.
Last edited on
Topic archived. No new replies allowed.