Converting to a Template

I have a question that I have been trying to figure out for a few days, I don't really understand how to make this program compile. Firstly the program had all of the values set, but now I am required to make it a template based. I have a great list of errors(about 9) but I am just looking for some guidance from someone who has experience in this. Thanks for you help.

Header 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
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
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include <stdexcept>

using namespace std;

//Declaring constant
const int VECTOR_CAP = 2;

template <class T>
class myVector
{
private:
	//Setting data members
	T* vectorData;
	int cap;
	int numElements;

public:
	//Default constructor
	//Purpose: Creates a vector
	//Parameters: None
	//Returns: None
	myVector();

	//Parameterized constructor
	//Purpose: Creates a vector capacity of n
	//Parameters: None
	//Returns: None
	myVector(const T&);

	//Copy Constructor
	//Purpose: Copy data into vector
	//Parameters: myVector object
	//Returns: None
	myVector(const T& copy)
	{
		numElements = copy.numElements;
		vectorData = new int[numElements];
		for (int i = 0; i < numElements; i++)
		{
			this->vectorData[i] = copy.vectorData[i];
		}
	}

	//Destructor
	//Purpose:Deletes any dynamically allocated storage
	//Parameters: None
	//Returns: None
	~myVector();

	//Size function
	//Purpose: returns the size of your vector
	//Parameters: None
	//Returns: The size of your vector as an integer
	T size() const;

	//Capacity function
	//Purpose: Returns the capacity of the vector
	//Parameters: None
	//Returns: Maximum value that your vector can hold
	T capacity() const;

	//Clear function
	//Purpose: Deletes all of the elements from the vector and resets its size to zero and its capacity to two; thus becomming empty
	//Parameters: None
	//Returns: None
	void clear();

	//push_back function
	//Purpose: Adds the integer value n to the end of the vector
	//Parameters: Takes a integer to be placed in the vector
	//Returns: None
	void push_back(const T&);

	//at function
	//Purpose: Returns the value of the element at position n in the vector
	//Parameters: None
	//Returns: Returns your current place with the vector
	T at(T&) const;

	//assignment
	//Purpose: Overload the = operator
	//Parameters: The two myVector objects we want to assign
	//Returns: The assignment
	myVector operator=(const myVector&);

	void pop_back();

	int last();

	myVector operator[](T&);

};

//Independant Functions

//streamInsertionOverload
//Purpose: Overload the stream insertion operator
//Parameters: The stream object to read from, and object to read to
//Returns:The stream
//template <typename T>
//ostream& operator<<(ostream& out, const T& rho);

template <typename T>
int myVector<T>::capacity() const
{
	return cap;
}

template <typename T>
int myVector<T>::pushback(T&) const
{
	//If statement to handle if array is full 
	if (numElements == cap)
	{
		//Doubling the capacity 
		cap = cap * VECTOR_CAP;
		//Allocating new array 
		int* newVectorData = new int[cap];
		//Copying data
		for (int i = 0; i < numElements; i++) newVectorData[i] = vectorData[i];
		//Deleting previous data
		delete[] vectorData;
		//Pointing to new data
		vectorData = newVectorData;
	}
	//Storing data
	vectorData[numElements++] = n;
}

template <typename T>
ostream& operator<<(ostream& out, const myVector<T>& rho)
{
	for (int n = 0; n < rho.size(); n++)
	{
		out << rho.at(n);
	}
	return out;
}


Driver 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
#include <iostream>
#include "vectorHeader.h"
using namespace std;

int main()
{
	const char START = 'A';
	const int MAX = 12;

	// create a vector of doubles
	myVector<char> vectD;

	// push some values into the vector
	for (int i = 0; i < MAX; i++)
	{
		vectD.push_back(START + i);
	}

	// remove the last element
	vectD.pop_back();

	// add another value
	vectD.push_back('Z');

	// test memory management
	myVector<char> vectD2 = vectD;
	// display the contents
	cout << "\n[";
	for (int i = 0; i < vectD2.size() - 1; i++)
	{
		cout << vectD2[i] << ", ";
	}

	cout << "..., " << vectD2.last() << "]\n";


	system("PAUSE");
	return 0;

}


Error list

error C2679: binary '[' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) LINE 31 (driver)
6 IntelliSense: no operator "[]" matches these operands
operand types are: myVector<char> [ int ] LINE 31 (driver)
error C2535: 'myVector<T>::myVector(const T &)' : member function already defined or declared LINE 39 (header)
error C2244: 'myVector<T>::capacity' : unable to match function definition to an existing declaration LINE 112 (header)
error C2039: 'pushback' : is not a member of 'myVector<T>' LINE 133 (header)
error C2535: 'myVector<char>::myVector(const T &)' : member function already defined or declared LINE 39 (header)
Last edited on
1
2
3
4
5
	//Size function
	//Purpose: returns the size of your vector
	//Parameters: None
	//Returns: The size of your vector as an integer
	T size() const;
`T' is not an integer
T may be anything, like a string, a person, an integer. ¿what would be the meaning of saying that your size is Mitsubishi?
You have that error in several places. (at(), capacity(), operator[])


> 'myVector<T>::myVector(const T &)' : member function already defined or declared
¿what's the difference between line 33 and 39?


> 'pushback' : is not a member of 'myVector<T>'
typo. You've declared push_back


> Error list
¿your crappy compiler does not provide line numbers?
Last edited on
I have updated the list to include the line numbers, I will fix what you have pointed out. Thanks for your help. Let me know if you see anything else with the line numbers provided.
Okay taking into consideration of all of the things said above, I have narrowed it down to one statement error, and I don't know how to declare it as a 'T'. The error is on line 42 when I declare a new int. How do I declare it so that the compiler allows it? thanks for your help. I also want to know if there is any other things that I can fix to look better. I will provide the header code because the driver code hasn't changed

header

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
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include <stdexcept>

using namespace std;

//Declaring constant
const int VECTOR_CAP = 2;

template <class T>
class myVector
{
private:
	//Setting data members
	T* vectorData;
	int cap;
	int numElements;

public:
	//Default constructor
	//Purpose: Creates a vector
	//Parameters: None
	//Returns: None
	myVector();

	//Parameterized constructor
	//Purpose: Creates a vector capacity of n
	//Parameters: None
	//Returns: None
	myVector(const T&);

	//Copy Constructor
	//Purpose: Copy data into vector
	//Parameters: myVector object
	//Returns: None
	myVector(const myVector& copy)
	{
		numElements = copy.numElements;
		vectorData = new int [numElements];
		for (int i = 0; i < numElements; i++)
		{
			this->vectorData[i] = copy.vectorData[i];
		}
	}

	//Destructor
	//Purpose:Deletes any dynamically allocated storage
	//Parameters: None
	//Returns: None
	~myVector();

	//Size function
	//Purpose: returns the size of your vector
	//Parameters: None
	//Returns: The size of your vector as an integer
	int size() const;

	//Capacity function
	//Purpose: Returns the capacity of the vector
	//Parameters: None
	//Returns: Maximum value that your vector can hold
	int capacity() const;

	//Clear function
	//Purpose: Deletes all of the elements from the vector and resets its size to zero and its capacity to two; thus becomming empty
	//Parameters: None
	//Returns: None
	void clear();

	//push_back function
	//Purpose: Adds the integer value n to the end of the vector
	//Parameters: Takes a integer to be placed in the vector
	//Returns: None
	void push_back(const T&)
	{
		//If statement to handle if array is full 
		if (numElements == cap)
		{
			//Doubling the capacity 
			cap = cap * VECTOR_CAP;
			//Allocating new array 
			int* newVectorData = new int[cap];
			//Copying data
			for (int i = 0; i < numElements; i++) newVectorData[i] = vectorData[i];
			//Deleting previous data
			delete[] vectorData;
			//Pointing to new data
			vectorData = newVectorData;
		}
		//Storing data
		vectorData[numElements++] = n;
	}

	//at function
	//Purpose: Returns the value of the element at position n in the vector
	//Parameters: None
	//Returns: Returns your current place with the vector
	int at(T&) const;

	//assignment
	//Purpose: Overload the = operator
	//Parameters: The two myVector objects we want to assign
	//Returns: The assignment
	myVector operator=(const myVector&);

	void pop_back();

	int last();

	myVector operator[](const myVector&);

};

//Independant Functions

template <typename T>
int myVector<T>::capacity() const
{
	return cap;
}

template <typename T>
ostream& operator<<(ostream& out, const myVector<T>& rho)
{
	for (int n = 0; n < rho.size(); n++)
	{
		out << rho.at(n);
	}
	return out;
}
1
2
//vectorData = new int [numElements];
vectorData = new T [numElements];


As an advice, you could have done
1
2
3
4
5
6
7
8
9
10
11
12
13
class myVector{
public:
   typedef int value_type;
   //using value_type instead of int where it should
   //...
};
//then convert to template
template<class T>
class myVector{
public:
   typedef T value_type;
   //the rest remain the same
};



By the way, don't using namespace in a header
Last edited on
oh I see, that makes sense. When I switched the int to T, I now have a great deal of undeclared identifiers on my driver code. Here are the errors, what can I do to fix these.

Error 8 error C2065: 'vectD' : undeclared identifier
Error 9 error C2065: 'vectD' : undeclared identifier
Error 10 error C2065: 'vectD' : undeclared identifier
Error 11 error C2065: 'vectD' : undeclared identifier
Error 12 error C2065: 'cout' : undeclared identifier
Error 13 error C2065: 'cout' : undeclared identifier
Error 14 error C2065: 'cout' : undeclared identifier
Error 1 error C2143: syntax error : missing ';' before '&'
Error 2 error C2065: 'ostream' : undeclared identifier
Error 3 error C2065: 'out' : undeclared identifier
Error 4 error C2988: unrecognizable template declaration/definition
Error 5 error C2059: syntax error : 'const'
Error 6 error C2065: 'T' : undeclared identifier
Error 7 error C2923: 'myVector' : 'T' is not a valid template type argument for parameter 'T'\
Topic archived. No new replies allowed.