template class and C2244 error

i am writing a program for general array to be used for various types
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//Array.h
#ifndef Array_H
#define Array_H
template<typename T>
class Array
{
public: 
	Array();
	Array(int size);
	Array(const Array& rhs);
	~Array();
	void destroy();
	Array& operator =(const Array & rhs);
	void resize(int newsize);
	int size();
	float& operator [](int i);
private:
	
	T* mData;
	int mSize;
};
#endif 

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
//Array.cpp
#include "Array.h"
#include<iostream>
using namespace std;
template<typename T>
Array<T>::Array()
{
	mData=0;
}
template<typename T>
void Array<T>:: resize(int newSize)
{
	destroy();
	mSize = newSize;
	mData = new  T[mSize];
	for(int i=0;i<mSize;i++)
		mData[i]=0;
}
template<typename T>
Array<T>::Array(int size)
{
	mData=0;
	resize(size);
}
template<typename T>
Array<T>::Array(const Array& rhs)
{
	mData=0;mSize=0;
	*this=rhs;
}
template<typename T>
void Array<T>::destroy()
{
	delete[] mData;
	mData=0;
	mSize=0;
}
template<typename T>
Array<T>::~Array()
{
	destroy();
}
template<typename T>
Array<T>& Array<T>::operator=(const Array<T>& rhs)
{
	if(this==&rhs)return *this;
	resize(rhs.mSize);
	for (int i =0;i<rhs.mSize;i++)
		mData[i]=rhs.mData[i];
	return *this;
}
template<typename T>
int Array<T>::size()
{
	return mSize;
}
template<typename T>
T& Array<T>::operator[](int i)
{
	return mData[i];
}

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
//main.cpp
#include "Array.h"
#include<iostream>
#include"Array.cpp"
using namespace std;
template <typename T>
void printArray(Array<T>& fa)
{
	cout<<"{";
	for(int i=0;i<fa.size();i++)
		cout<<fa[i]<<" ";
	cout<<"}"<<endl;
}
void main()
{
	Array<float> A;
	A.resize(4);
	A[0]=1.0f;
	A[1]=2.0f;
	A[2]=3.0f;
	A[3]=4.0f;
	cout<<"printing A:";
	printArray(A);
	Array<float> B(A);
	cout<<"printing B:";
	printArray(B);
	Array<float> C=B=A;
	cout<<"printing C:";
	printArray(C);
	A=A=A=A;
	cout<<"printing A:";
	printArray(A);
	Array<int> b;
	b.resize(3);
	b[0]=1;
	b[1]=2;
	b[2]=3;
	cout<<"printing int array:";
	printArray(b);


}

it is giving following error
error C2244: 'Array<T>::operator []' : unable to match function definition to an existing declaration. what am i doing wrong here??
I think the return type in the definition declaration of that function should be "T" not "float".

Edit: It was late but cniper figured out what I meant.
Last edited on
oh man!!! my bad!!
Topic archived. No new replies allowed.