CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    Join Date
    Jan 2006
    Posts
    344

    [RESOLVED] Assignment on function

    Since you can't use [][] to access a matrix you are suggested to use () operator instead.

    However a limitation of that is you can't assign:
    Code:
    m[col][row] = x; // valid
    m(col, row) = x; // invalid
    which forces us to write such statements as:
    Code:
    m[( col * cols ) + row] = rhs( col, row );
    Is there any way of working around that limitation so you can use
    Code:
    m( col, row ) = rhs( col, row );
    which is more elegant?

    It's mainly an academic exercise as I've finished the matrix class and just having a wonder over lunch.

  2. #2
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: Assignment on function

    you can do an assign with the m(x,y) notation.

    There should be two implementations of this function. I will assume double
    for the example

    Code:
    double operator()(const size_t row, const size_t col) const;
    double& operator()(const size_t row, const size_t col);
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  3. #3
    Join Date
    Jan 2006
    Posts
    344

    Re: Assignment on function

    Well apart from the fact I use float and index in using int's ours are identical.

    Actual code:
    Code:
    	float& operator()( const int col, const int row )
    	{
    		return m[( col * cols ) + row];
    	}
    	
    	float operator()( const int col, const int row ) const
    	{
    		return m[( col * cols ) + row];
    	}
    However I get the error telling me that the "term does not evaluate to a function".

  4. #4
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: Assignment on function

    well, that should work, so something else is wrong
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  5. #5
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Assignment on function

    You'll have to show us the full code so we can see WHAT isn't evaluating to a function.

    BTW, shouldn't it be col + (row * cols) ?
    My hobby projects:
    www.rclsoftware.org.uk

  6. #6
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Assignment on function

    You need to make them member functions, of course:

    Code:
    template<typename T>
    class Array2D
    {
       std::vector<T> Data_;
       unsigned int Row_;
       unsigned int Cols_;
       
    public:
       Array2D( unsigned int Rows, unsigned int Cols ) :
          Rows_( Rows ),
          Cols_( Cols ),
          Data_( Rows * Cols )
       {
       }
    
       const T& operator()( unsigned int Row,  unsigned int Col ) const
       {
          return Data[Row * Cols_ + Col];
       }
    
       T& operator()( unsigned int Row,  unsigned int Col )
       {
          return Data[Row * Cols_ + Col];
       }
    };
    - Guido

  7. #7
    Join Date
    Jan 2006
    Posts
    344

    Re: Assignment on function

    Quote Originally Posted by Zaccheus
    You'll have to show us the full code so we can see WHAT isn't evaluating to a function.
    Okay. How about setting up and identity matrix in the constructor?
    Code:
    	for ( int col=0; col<cols; col++ ) 
    	{
    		for ( int row=0; row<rows; row++ ) 
    			m( col, row ) = 0.0f;
    		m( col, row ) = 1.0f;
    	}
    Quote Originally Posted by Zaccheus
    BTW, shouldn't it be col + (row * cols) ?
    No, I'm pretty sure column-major is (col * cols) + row.

  8. #8
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: Assignment on function

    Quote Originally Posted by grahamr (work)
    No, I'm pretty sure column-major is (col * cols) + row.
    Nope.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  9. #9
    Join Date
    Jan 2006
    Posts
    344

    Re: Assignment on function

    I thought column-major layout is
    Code:
    [0  4  8 12]
    [1  5  9 13]
    [2  6 10 14]
    [3  7 11 15]
    G.

  10. #10
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Assignment on function

    Oh I see.

    I think it is better for each dimension to be contiguous memory.
    My hobby projects:
    www.rclsoftware.org.uk

  11. #11
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Assignment on function

    Quote Originally Posted by grahamr (work)
    Okay. How about setting up and identity matrix in the constructor?
    Code:
    	for ( int col=0; col<cols; col++ ) 
    	{
    		for ( int row=0; row<rows; row++ ) 
    			m( col, row ) = 0.0f;
    		m( col, row ) = 1.0f;
    	}
    What is 'm' ? How is it defined/created?
    My hobby projects:
    www.rclsoftware.org.uk

  12. #12
    Join Date
    Jan 2006
    Posts
    344

    Re: Assignment on function

    Quote Originally Posted by Zaccheus
    What is 'm' ? How is it defined/created?
    Code:
    float m[16];

  13. #13
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: Assignment on function

    if for whatever reason you want to do column major layout (C uses row major layout in memory) then you would do

    col*rows + row
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  14. #14
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Assignment on function

    Quote Originally Posted by grahamr (work)
    Code:
    float m[16];
    'm' should be a 2D matrix class such as in GNiewerth's post.
    Code:
     Array2D m(4, 4);
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  15. #15
    Join Date
    Jan 2006
    Posts
    344

    Re: Assignment on function

    Does it matter what it is? It doesn't change the fact that using () to index into the array doesn't allow assignment (or I can't use assignment).

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured