Operator []

Hey guys, i can't figure out how i do the overloading of the operator []
this is my code and what i tried to do:
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
using namespace std;

template<typename T, size_t Tlinhas, size_t Tcolunas>
class Matriz
{
private:
	T dados[Tlinhas][Tcolunas];
public:
	Matriz()
	{
		for(int l=0; l< Tlinhas; l++)
			for(int c=0; c< Tcolunas; c++)
				dados[l][c] = 0;
	}
	T& operator[](int i)
	{
		return dados[i];
	}
	const T& operator[](int i) const
	{
		return dados[i];
	}

	template<typename TT, size_t TTlinhas, size_t TTcolunas>
	friend ostream& operator<<(ostream&, const Matriz<TT, TTlinhas, TTcolunas>&);

};

template<typename T, size_t Tlinhas, size_t Tcolunas>
ostream& operator<<(ostream &out, const Matriz<T, Tlinhas, Tcolunas>& obj)
{
	for(int l=0; l<= Tlinhas; l++)
	{
		for(int c=0; c<= Tcolunas; c++)
		{
			out << obj[l][c] << " ";//Error here
		}	
		out << endl;
	}
	return out;
}
///////////MAIN/////////////////
int _tmain(int argc, _TCHAR* argv[])
{
	Matriz<int, 5, 5> M;
	cout << M;
	return 0;
}

the error i got is:
error C2109: subscript requires array or pointer type
if i take out the const overloading the error changes to:
error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const Matriz<T,Tlinhas,Tcolunas>' (or there is no acceptable conversion)

i will be very thankful if anyone could help me.
Last edited on
Topic archived. No new replies allowed.