-1

I'm used to C# and Matlab and I'm trying to do a simple C++ program using Eigen template library for linear algebra.

Sorry but I'm struggling with the basics:

  • Store Matrix3d Eigen in struct
  • Pass as parameter to a method
  • Return a struct containing Eigen objects references.

Sample code (it is not compiling, C# developer in pain here):

struct MyStruct
{
    Matrix3d* K1; //Is that the right way, using pointers?
    Matrix3d* K2;
};

int main()
{
    //Case 1
    MyStruct* A = new MyStruct();
    A->K1 = &(Matrix3d)Matrix3d::Random();

    MyStruct* result = MyMethod(A);

    //Case 2 - I noticed that everybody uses case 1, what is the difference? 
    MyStruct B;
    B.K1 = &(Matrix3d)Matrix3d::Random();
    B.K1(0,0) = 1; // Not working. How can I access it?

    MyStruct* result = MyMethod(&B);
}

*MyStruct DoSomething(MyStruct& input)
{
    MyStruct result;
    result.K1 = ...
    return &result;
}
Galik
  • 47,303
  • 4
  • 80
  • 117
Pedro77
  • 5,176
  • 7
  • 61
  • 91
  • 1
    I don't see any reason to use pointers in this. Always use values unless you have a compelling reason not to. – Galik Mar 11 '17 at 21:13
  • @Galik You mean inside the structure? I'm trying to prevent the huge matrix object to be copied when I call the method, but if I pass a reference to the structure is enough. And what about the "B.K1(0,0) = 1;"? – Pedro77 Mar 11 '17 at 21:24
  • If need be pass the whole `struct` (or individual matrices) by (const) reference or pointer to avoid copying it. I'm not familiar enough with the `Eigen` library to give a fuller answer. – Galik Mar 11 '17 at 21:37
  • You should properly learning C++ by reading a good book before trying to implement something non-trivial. – Vittorio Romeo Mar 11 '17 at 21:37
  • One of the first things you need to do for your code to compile is `#include` your headers, in this case `#include `. Then, you want to add `using namespace Eigen;` below the `#include` lines but ahead of any other code. At that point, I would try copying and pasting a code sample from https://eigen.tuxfamily.org/dox/GettingStarted.html, and once you have those working, modify them to do what you want. Make sure each modification compiles. – Davislor Mar 11 '17 at 21:55
  • 1
    That said, you don’t ever want to create a `new` object and not `delete` it (If you want a pointer that will be deleted automatically when it goes out of scope, that’s what `unique_ptr` is for, but here, you can just declare the object.) There’s no particular reason you need a `struct`; you can pass two arguments. If you do need an object that’s just a pair of matrices, there’s `std::pair`. Finally, it is an error to `return &result;` in `DoSomething()`, because `result` is a temporary object and will not exist on return! – Davislor Mar 11 '17 at 22:03
  • 1
    To fix the `return &result;` bug, the simplest approach is to `return result;` (which, with more advanced techniques, you can make the receiving code handle efficiently by moving and not copying). Or, you could construct the result in place with what’s called a programmatic constructor;` What I suggest you learn to do first, though, is to pass `MyStruct result&` as a parameter that tells your function where to store its results. Then it does the computation in place and does not make inefficient copies. – Davislor Mar 11 '17 at 22:12
  • @Davislor was going to ask you just that! And I started from a Eigen sample, just did'nt added all the header and stuff here. – Pedro77 Mar 11 '17 at 22:14
  • I suggest you post a MCVE; when you leave out the required boilerplate and then say the code doesn’t compile, that looks suspicious. :) – Davislor Mar 11 '17 at 22:18

1 Answers1

0

Not familiar with Eigen. But I can give you some general advice about programming in C++.

  • Store Matrix3d Eigen in struct

If you need to allocate Matrix3d objects, I recommend doing resource management in the constructor and destructor of your struct or class.

Also trying to use smart pointer insteand of C type pointer.

Ref: RAII.

  • Pass as parameter to a method

Usually prefer pass by reference (or const reference if you can).

  • Return a struct containing Eigen objects references.

The return value really depends. You may not need a return value if you only modify on the input reference, or you can just return by value if the MyStruct object is small.

In all, as the comments said, you need the basic idea and implemention style of resource management in C++. You may want to find some books and learn these topics.

Xu Han
  • 89
  • 1
  • 4