Search code examples
matrixc++-clieigen

C++ CLI Eigen: setting values in Matrix


This is my code.

Matrix<int, 240, 240>* imagePixels;
    for (signed int x = 0; x < 100; ++x)
            {
                for (signed int y = 0; y < 100; ++y)
                {   
                    imagePixels(x,y) = y;
                }
            }

I want to simply add values to my matrix but it gives me:

expression preceding parentheses of apparent call must have (pointer-to-) function type

at matrix(x,y) = y; I'm using C++ CLI.


Solution

  • I've never used Eigen, but I think it's complaining about the type of imagePixels.

    Matrix<int, 240, 240>* imagePixels;
    

    (I'm assuming the fact that you didn't initialize imagePixels with anything is a copy-paste error on the web, not in your actual code.)

    All the examples of using the () syntax to access Eigen matrix objects are using the class a value type, not a pointer. Try it without the *, and see if that solves it for you.

    Matrix<int, 240, 240> imagePixels;
    //                   ^-- No "*"
    

    Edit

    OK, so imagePixels is a member of your managed class. Managed classes are only allowed to contain other managed classes, managed handles (^), unmanaged pointers (*), or basic types (e.g., int). Unmanaged classes as a value type is not allowed.

    There's two ways around this:

    1. Leave imagePixels as a pointer, and dereference it each time you use the () syntax.

      (*imagePixels)(x,y) = y;
      
    2. Declare a unmanaged struct to hold your matrix as a value, and have a pointer to that in your class.

      struct HolderOfUnmanagedThings { Matrix<int, 240, 240> imagePixels; };
      
      // In your managed class
      HolderOfUnmanagedThings* unmanaged = new HolderOfUnmanagedThings();
      
      unmanaged->imagePixels(x,y) = y;