dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Calling on an element of a matrix class

  1. #1
    Join Date
    Dec 2010
    Posts
    114

    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;
    }

  2. #2
    Join Date
    Feb 2017
    Posts
    344

    Re: Calling on an element of a matrix class

    Quote Originally Posted by luckiejacky View Post
    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.

  3. #3
    Join Date
    Dec 2010
    Posts
    114

    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 low-end 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

  4. #4
    Join Date
    Feb 2017
    Posts
    344

    Re: Calling on an element of a matrix class

    Quote Originally Posted by luckiejacky View Post
    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 low-end 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.

  5. #5
    Join Date
    Dec 2010
    Posts
    114

    Re: Calling on an element of a matrix class

    Thanks

  6. #6
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,115

    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];
    };
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)