
June 2nd, 2018, 12:11 AM
#1
Calling on an element of a matrix class
The line:
Code:
sum += (gauss[x + 2][y + 2] = exp((x * x + y * y) / s) / s / M_PI);
cannot be compiled.
which says:
Code:
1>e:\projects\c++\edge_detect\edge_detect\Matrix.h(14): error C2059: syntax error : '['
1> e:\projects\c++\edge_detect\edge_detect\Matrix.h(17) : see reference to class template instantiation 'matrix<T,D0,D1>' being compiled
How can I access the following class by the [] or [][] operator and access the matrix by row or element?
Code:
template<typename T, int D0, int D1>
class matrix
{
public:
matrix();
matrix(const D3DXVECTOR2& m0, const D3DXVECTOR2& m1);
matrix(const D3DXVECTOR3& m0, const D3DXVECTOR3& m1, const D3DXVECTOR3& m2);
float[4]& operator[](int row) const;
float m[4][4];
};
Code:
matrix<float, 5, 5> gaussian_kernel(float sigma)
{
matrix<float, 5, 5> gauss;
float sum = 0, s = 2 * sigma * sigma;
for (int x = 2; x <= 2; x++)
for (int y = 2; y <= 2; y++)
sum += (gauss[x + 2][y + 2] = exp((x * x + y * y) / s) / s / M_PI);
for (auto& row : gauss)
for (auto& x : row)
x /= sum;
return gauss;
}

June 2nd, 2018, 01:34 AM
#2
Re: Calling on an element of a matrix class
Originally Posted by luckiejacky
The line:
Code:
sum += (gauss[x + 2][y + 2] = exp((x * x + y * y) / s) / s / M_PI);
cannot be compiled.
Since the matrix class represents no abstraction why not access the m member, like
Code:
sum += (gauss.m[x + 2][y + 2] = exp((x * x + y * y) / s) / s / M_PI);
Last edited by wolle; June 2nd, 2018 at 01:41 AM.

June 2nd, 2018, 01:52 AM
#3
Re: Calling on an element of a matrix class
The original definition of the matrix is:
Code:
template<class T, size_t Rows, size_t Cols> using matrix = std::array<std::array<T, Cols>, Rows>;
I was unable to compie this with some lowend compilers. Should I use my method to adapt to this class, or should I seek another workaround to make this matrix class work again?
thanks
Jack

June 2nd, 2018, 02:30 AM
#4
Re: Calling on an element of a matrix class
Originally Posted by luckiejacky
The original definition of the matrix is:
Code:
template<class T, size_t Rows, size_t Cols> using matrix = std::array<std::array<T, Cols>, Rows>;
I was unable to compie this with some lowend compilers. Should I use my method to adapt to this class, or should I seek another workaround to make this matrix class work again?
That's a nice solution and it allows for [][] accesses. It compiles with the latest VS 2017 compiler, can't you upgrade?
If not, your current matrix class approach will work with an older compiler, like
Code:
template<class T, size_t R, size_t C> // you possibly need to use int rather than size_t
class matrix {
public:
T data[R][C];
};
//
matrix<int, 4, 4> gauss;
gauss.data[0][0] = 1; // works
Last edited by wolle; June 2nd, 2018 at 02:34 AM.

June 2nd, 2018, 03:06 AM
#5
Re: Calling on an element of a matrix class

June 2nd, 2018, 08:54 PM
#6
Re: Calling on an element of a matrix class
A way to make your operator[] work is to implement it as follows:
Code:
template<typename T, int D0, int D1>
class matrix
{
public:
matrix() {};
T* operator[](int row) { return m[row]; }
T m[4][4];
};
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
OnDemand Webinars (sponsored)
