Cannot convert argument 'int' to 'char &'

I am getting a compiler error and I don't know how to fix it. I know that I need to declare something as a template just not sure where I am going wrong. Thanks for your help in advance.

Driver
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
#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;

}


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


//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 T [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& n)
	{
		//If statement to handle if array is full 
		if (numElements == cap)
		{
			//Doubling the capacity 
			cap = cap * VECTOR_CAP;
			//Allocating new array 
			T* newVectorData = new T[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
	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[](const myVector&);



};

//Independant Functions

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

template <typename T>
myVector<T> myVector<T>::operator=(const myVector& rho)
{
	//Test for assingment
	if (this == &rho)
	{
		return *this;
	}

	//Delete lho
	delete[] this->vectorData;

	//Creating new array to fit rho data
	cap = rho.cap;
	this->vectorData = new int[cap];

	//Copying data
	for (int i = 0; i < numElements; i++)
	{
		this->vectorData[i] = rho.vectorData[i];
	}

	//Returning myVector object
	return *this;
}


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

Error 1 error C2664: 'char myVector<char>::at(T &) const' : cannot convert argument 1 from 'int' to 'char &' LINE 158 (header)
Can anyone give me some help on this one?
At line 100 you've declared at() as T at(T&) const;. But the index of an array is a number, not a variable of the type of the array. Also, at() should return a reference to the value in the array, not a copy of it. That way the caller can change the value. In order to return a reference, it needs to be called on a non-const instance of the vector, So it should be T& at(size_t);

Note that operator[] should be declared nearly the same way: it takes a size_t, returns a T& and is not const. Since operator[] and at() at so closely related, the definition of one should use the other.

Thank you for your quick response! I am now receiving another error after I make the change to the at function, it reads: Error 1 error C2662: 'char &myVector<char>::at(size_t)' : cannot convert 'this' pointer from 'const myVector<char>' to 'myVector<char> &'. When you say to return the values in what way would you return them? in a implementation of the template, or within the header public space?
This is the implementation that I have tried but it isn't working, I am not sure what I can call with the size_t function.
1
2
3
4
5
template <typename T>
int myVector<T>::at(size_t n)
{
	return vectorData[n];
}
And here is the definition that I am trying for the [] operator that you have suggested, and I am getting the same error. What would be your suggestion dhayden?

T& operator[](std::size_t);

1
2
3
4
5
template <typename T>
myVector<T> myVector<T>::operator[](std::size_t)
{
	return T&;
}
1
2
3
4
5
template <typename T>
T& myVector<T>::operator[](std::size_t index)
{
	return vectorData[index];
}
Topic archived. No new replies allowed.