0

When the Matrix argument is defined as constant I get

.resize(rows,cols) Matrix const MatrixXd as 'this' argument discards qualifiers error

    void (const Eigen::MatrixXd &X){
    X.resize(cols, rows) }

returns an error but this works as expected:

    void(Eigen::MatrixXd &X){
    X.resize(cols, rows)}

I'm not too familiar with c++ (other than using it for this class) and am wondering what this means?

Thanks for any pointers.

JHBonarius
  • 10,824
  • 3
  • 22
  • 41
Olli
  • 906
  • 10
  • 25
  • 1
    Does this answer your question? [Sell me on const correctness](https://stackoverflow.com/questions/136880/sell-me-on-const-correctness) – JHBonarius Jan 11 '22 at 07:46
  • See the dupe: I think you should start with a C++ primer, because this is a very basic principle of C++. Don't use a chainsaw until you had some lessons, or you'll cut off your arm. – JHBonarius Jan 11 '22 at 07:48
  • @JHBonarius thanks, I went through some basics but I hope I'll never see it again after this course. I vaguely remember from my operating sys class last year thx – Olli Jan 11 '22 at 08:26
  • @JHBonarius should I delete the post? I originally thought this was an Eigen error but the answer below actually jogged my memory a little. Also my course lecturer actually thinks we don't need a c++ primer to use eigen and these methods. Maybe some day later they'll find this and rethink as most students would have preferred an introduction to a new language. – Olli Jan 11 '22 at 08:38
  • IMHO C++ is a beautiful language and knowing it and its paradigms adds value to your way of thinking. I am sorry that you seem to have (a) bad teacher(s). They ruin the fun and joy it can bring. – JHBonarius Jan 11 '22 at 09:35

1 Answers1

1

The warning means that the resize function that you called is not const qualified. The lack of const qualification means that the function cannot be called on a const lvalue. resize is a function that modifies the object. The rough meaning of "const" is that modification isn't allowed.

X is an lvalue reference to const, so non-const qualified functions cannot be called through the reference. You attempted to call a non-const qualified function through the const reference. Since that's not allowed, the compiler told you about the bug.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Why not tag a dupe? Seeing the OP is a C++ beginner, you might want to expand on what 'const' and 'lvalue' is. Those are not general concepts in all languages. – JHBonarius Jan 11 '22 at 07:53
  • @JHBonarius I rarely vote since my high rep won't let anyone else give input; the question will close immediately. I'll just get complaints for closing with duplicates that aren't exactly the same as the question. If you can find a duplicate, then feel free to vote yourself. – eerorika Jan 11 '22 at 07:58
  • Thanks a lot for this answer, I vaguely remember this concept from C. Sorry if this was a duplicate, I thought this was Eigen and not c++. – Olli Jan 11 '22 at 08:27