Unable to match functions definition

Pages: 12
I have two errors that I am not sure how to fix, please let me know what I need to fix. Here are the errors:
Error 1 error C2244: 'myVector<T>::at' : unable to match function definition to an existing declaration
Error 2 error C2244: 'myVector<T>::operator []' : unable to match function definition to an existing declaration

I know that it is in the implementation for both the 'at' and 'operator[] overload'.

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
162
163
164
165
166
167
168
169
170
171
#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(std::size_t);

	//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();

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

};

//Independant Functions

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

template <typename T>
int myVector<T>::at(std::size_t n)
{
	return vectorData[n];
}

template <typename T>
myVector<T> myVector<T>::operator[](std::size_t)
{
	return T&;
}

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 return types in the prototypes and functions aren't matching.

T& at(std::size_t);
int myVector<T>::at(std::size_t n)

T& operator[](std::size_t);
myVector<T> myVector<T>::operator[](std::size_t) <-- missing parameter name here after std::size_t
so for instance

T& at(std::size_t);
T myVector<T>::at(std::size_t n) <---- still has the same error, I am not sure how to declare it

the same goes for the next.
and I am not certain what parameter is missing from the operator[] could you be specific? thank you for your help.
closed account (SECMoG1T)
Both should return T&
I see it's
T& myVector<T>::at(std::size_t n) <---- missing & from T declaration

I am not sure how to fix operator[] though, because there isn't a beginning declaration
Why would both of them return T& andy1992?
Last edited on
These functions are supposed to return a reference to the value of type T at the element n of the vector?
so for:

1
2
3
4
5
template <typename T>
T& myVector<T>::at(std::size_t n)
{
	return vectorData[n];
}


I need to remove the n from there completely and change return vectorData[n] to return T&?
Also your operator [] is missing the parameter name and you try to return T& which doesn't make sense. You should probably return vectorData[position]
That is what I was trying to understand from what andy1992 said, I am trying to get a position, I didn't understand both of them returning T&
This is dhayden's post from your other thread - I think this is why you were making the return type T&

http://www.cplusplus.com/forum/beginner/150169/#msg784342
Return value and return type are different. The return type should be T& and the return value would be the value at that position in the vector.

Also i just noticed, you posted right before I did
1
2
3
4
5
template <typename T>
T& myVector<T>::at(std::size_t n)
{
	return vectorData[n];
}
which you would do for the other one as well. Instead of at you would put operator[] THey shoudl look the same otherwise.
Last edited on
closed account (SECMoG1T)
Ooh I meant this

 
T& myVector<T>::operator[](std::size_t n) ///your declaration and definitions should have this form 
Okay this is all making a lot of sense thanks for walking me through this guys. The one thing that I don't understand is what operator[] should return, with n.

1
2
3
4
5
template <typename T>
T& myVector<T>::operator[](std::size_t n)
{
	return n;
}


This is what I have tried but I don't think that it is correct, should it be the same as the at function?
You should return the same thing you did with your at function.

The only difference is instead of at it is now operator[]

So line 4 should be return vectorData[n];
So when I add the code for the return vectorData[n] in my at function I get 6 more compiler errors:

Error 6 error LNK1120: 5 unresolved externals
Error 1 error LNK2019: unresolved external symbol "public: __thiscall myVector<char>::myVector<char>(void)" (??0?$myVector@D@@QAE@XZ) referenced in function _main
Error 2 error LNK2019: unresolved external symbol "public: __thiscall myVector<char>::~myVector<char>(void)" (??1?$myVector@D@@QAE@XZ) referenced in function _main
Error 3 error LNK2019: unresolved external symbol "public: int __thiscall myVector<char>::size(void)const " (?size@?$myVector@D@@QBEHXZ) referenced in function _main
Error 4 error LNK2019: unresolved external symbol "public: void __thiscall myVector<char>::pop_back(void)" (?pop_back@?$myVector@D@@QAEXXZ) referenced in function _main
Error 5 error LNK2019: unresolved external symbol "public: int __thiscall myVector<char>::last(void)" (?last@?$myVector@D@@QAEHXZ) referenced in function _main

I am not sure what these are referring to.
You have functions that you have prototypes for but no definitions yet.
That makes sense, so I still need definition for my constructor, destructor, size function, pop_back function, and last functions. I will post if I need more questions there. Thanks for everyone's help.
I am trying to do a pop_back function and am getting errors, I am not sure how to implement it properly, it is supposed to remove last integer pushed into vector, reducing its size by one, and if the vector is empty it will do nothing. Here is what I have so far.

1
2
3
4
5
6
7
8
9
template <typename T>
void myVector<T>::pop_back() 
{
	while (!myVector.empty())
	{
		sum += myVector.back();
		myVector.pop_back();
	}
}
This is the correct version that worked for me.

1
2
3
4
5
template <typename T>
void myVector<T>::pop_back() 
{
	numElements--;
}
Pages: 12