View difference between Paste ID: 55HTbehe and NnD9ZdfV
SHOW: | | - or go back to the newest paste.
1
// Matrix.hpp
2
#pragma once
3
4
namespace Furdarius
5
{
6
    const int MATRIX_MAX_SIZE = 100;
7
8
    struct Size {
9
	    int row, col;
10
    };
11
12
    template<typename _T>
13
    class Matrix
14
    {
15
        Size size;
16
        _T data[MATRIX_MAX_SIZE][MATRIX_MAX_SIZE];
17
18
    public:
19
        Matrix(void)
20
        {
21
            size.row = MATRIX_MAX_SIZE;
22
            size.col = MATRIX_MAX_SIZE;
23
        };
24
25
        Matrix(int row, int col)
26
        {
27
            size.row = row;
28
            size.col = col;
29
        };
30
31
        Matrix(Size _size)
32
        {
33
            size.row = _size.row;
34
            size.col = _size.col;
35
        };
36
37
        Matrix(Matrix &source, int num)
38
        {
39
            size = source.getSize();
40
            for (int i = 0; i < size.row; ++i)
41
                for (int j = 0; j < size.col; ++j)
42
                    data[i][j] = source[i][j] + num;
43
        };
44
45
        Size getSize() {
46
            return size;
47
        };
48
49
        _T* operator[](int id) {
50
            return data[id];
51
        };
52
53
        const _T* operator[](int id) const {
54
            return data[id];
55
        };
56
57
        operator double() const {
58
            double res = 0;
59
60
            for (int i = 0; i < size.row; ++i)
61
		        for (int j = 0; j < size.col; ++j)
62
			        res += data[i][j];
63
64
            return res / (size.row * size.col);
65
        };
66
67
        friend const Matrix<_T> operator+(const Matrix<_T>& left, const Matrix<_T>& right);
68
        friend const Matrix<_T> operator-(const Matrix<_T>& left, const Matrix<_T>& right);
69
70
        ~Matrix()
71
        {
72
            size.row = 0;
73
            size.col = 0;
74
        }
75
    };
76
77
    template<typename _T>
78
    const Matrix<_T> operator+(const Matrix<_T>& left, const Matrix<_T>& right) {
79
        Matrix<_T> res(left.getSize());
80
81
        for (int i = 0; i < res.getSize().row; ++i)
82
		        for (int j = 0; j < res.getSize().col; ++j)
83
			        res[i][j] = left[i][j] + right[i][j];
84
85
        return res;
86
    };
87
88
    template<typename _T>
89
    const Matrix<_T> operator-(const Matrix<_T>& left, const Matrix<_T>& right) {
90
        Matrix<_T> res(left.getSize());
91
92
        for (int i = 0; i < res.getSize().row; ++i)
93
		        for (int j = 0; j < res.getSize().col; ++j)
94
			        res[i][j] = left[i][j] - right[i][j];
95
96
        return res;
97
    };
98-
};
98+
};
99
100
// В main.cpp есть код сложения
101
Matrix<MatrixType> addRes = first + second;
102
// тут first и second это тоже матрицы Matrix<MatrixType>
103
104
/*
105
Линковщик ругается:
106
107
1>main.obj : error LNK2019: unresolved external symbol "class Furdarius::Matrix<int> const __cdecl Furdarius::operator+(class Furdarius::Matrix<int> const &,class Furdarius::Matrix<int> const &)" (??HFurdarius@@YA?BV?$Matrix@H@0@ABV10@0@Z) referenced in function _main
108
*/