Using Class Template in Visual C++

Hello,

I wrote template which return matrix in Window Form Application .My template is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
template<class T>
    class matrix1 {
    protected:  

    public:
          T *data;

            const unsigned rows, cols, size;
                matrix1(unsigned r, unsigned c) : rows(r), cols(c), size(r*c) {

          data = new T[size];
            }
            ~matrix1() { delete data; }
            void setValue(unsigned row, unsigned col, T value) { 
                    data[(row*cols)+col] = value;
            }
            T getValue(unsigned row, unsigned col) const {
                    return data[(row*cols)+col];
            }

};


I wrote this code in Main Project File in Windows Form Application.I defined 341*680 matrix with using this template :

matrix1<double>A(341,680);

I used function that do operation on this template and I defined it like this:

void function(matrix1<double> &b,array< double>^ data)

And call it:

function(A,temp);

(temp is one dimensinonal data array that I have to use for my programming algorithm)

For Example;When I want to print data that is located in the first row and first column.

Visual C++ recognise getvalue and setvalue function ,but couldn't print anything and gave a lot of error interested with matrix1 template

I tried this template and function on CLR Application and it worked.How could I do this On Windows Form Application?And Where should I locate template class on Windows Form Application?

Best Regards...







there shuld be no difrence for C++ templates in those project templates, since the last is just a project template of CLR Application (CLR empty project), however you must mind where you write your code, make sure your includes are in the right spot - did you include everyting? If that is not the case than elaborate on the subject.

Where shuld you locate templates? Same as any other C++, they must be declared before you create them but can be coded after.

This is an empty CLR project, compiles & runs.

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
template<class T>
class matrix1 {
protected:  

public:
      T *data;

        const unsigned rows, cols, size;
            matrix1(unsigned r, unsigned c) : rows(r), cols(c), size(r*c) {

      data = new T[size];
        }
        ~matrix1() { delete data; }
        void setValue(unsigned row, unsigned col, T value) ;
        T getValue(unsigned row, unsigned col) const {
                return data[(row*cols)+col];
        }

};



int main()
{
	matrix1<unsigned> A(11,12);
	A.setValue(10,9, int(-1));
    return 0;
}

template <class T>
void matrix1<T>::setValue(unsigned row, unsigned col, T value)
{ 
        data[(row*cols)+col] = value;
};
Topic archived. No new replies allowed.