Calling template function

Hello guys.I want to call function for my template but i don know how.I trried this syntax,but it is not true.
 
 int x = matrica1.GetElement<int>(2, 5);

I have template class for a matrix.I use matrix for type integer and complex.
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
 #pragma once
#include<stdio.h>
using namespace std;
template <class T>
class Matrica
{
	int vrste, kolone;
	T **p;
public:
	Matrica();
	Matrica(int a, int b);
	~Matrica();
	Matrica<T>& operator=(const Matrica& m);
	Matrica<T> GetElement(int x, int y){ return this->p[x][y]; }
	void SetElement(int x, int y, T newValue){ this->p[x][y] = newValue; }
	int GetX() { return vrste; }
	int GetY() { return kolone; }
	friend Matrica operator*(const Matrica&a, const Matrica &b);
};
template<class T>
Matrica<T>::Matrica()
{
	vrste = kolone = 12;
	p = new T*[12];
	for (int i = 0; i < 12; i++)
	{
		p[i] = new T[12];
		for (int j = 0; j < 12; j++)
			p[i][j] = 0;
	}

}
template<class T>
Matrica<T>::Matrica(int a, int b)
{
	vrste = a;
	kolone = b;
	p = new T*[a];
	for (int i = 0; i < a; i++)
	{
		p[i] = new T[b];
		for (int j = 0; j< b;j ++)
			p[i][j] = 0;
	}
}
template<class T>
Matrica<T>::~Matrica() {
	for (int i = 0; i < vrste; i++)
		delete[] p[i];
	delete[] p;

}

template<class T>
 Matrica<T>& Matrica<T>::operator=(const Matrica<T> & m)
{
	 for (int i = 0; i < vrste; i++)
		 delete[] p[i];
	 delete[] p;
	 kolone = m.kolone;
	 vrste = m.vrste;
	 p = new T*[vrste];
	 for (int i = 0; i < vrste; i++)
		 p[i] = new T[kolone];
		 for (int i = 0; i < vrste; i++)
			 for (int j = 0; j< kolone; j++)
				 p[i][j] = m.p[i][j];
	 return *this;

}
 template<class T>
 Matrica<T> operator*(const Matrica<T> &a, const Matrica<T>&b)
 {
	 int i, j,k;
	 if (a.vrste != b.kolone)
		 cout << "Matrice se ne mogu pomnoziti" << endl;
	 else {
		 Matrica<T> c;
		 c.p = new T*[a.vrste];
		 for (i = 0; i < a.vrste; i++)
		 {
			 c.p[i] = new T[b.kolone];
			 for ( j = 0; j <b.kolone; j++)
				c.p[i][j] = 0;
		 }
		 for (i = 0; i < a.vrste; ++i)
			 for (j = 0; j < b.kolone; ++j)
				 for (k = 0; k < a.kolone; ++k)
				 {
					 c.p[i][j] += a.p[i][k] * b.p[k][j];
				 }

		 return c;

	 }


}




Source.cpp
1
2
3
4
5
6
7
8
9
#include"Complex.h"
#include"Matrica.h"
void main()
{
	Matrica<int> matrica1;
	Matrica<Complex> matrica2(4, 7);
	int x = matrica1.GetElement<int>(2, 5);//Mistake ? how to do it
	system("pause");
}
Last edited on
1
2
3
T GetElement(int x, int y){ return this->p[x][y]; }

int x = matrical.GetElement(2, 5);
Topic archived. No new replies allowed.