Registered Member
|
Hi. I am having some difficulty writing templated classes that have Eigen matrices as members, and writing function members to those classes. Consider the code that is pasted below. In it, there are two classes that do the same thing: store references to A (a lower triangular matrix) and x (a vector). The myFunc function solves A'x for x, and returns the result.
The difference between them is that MyClass is templated, but MyClass2 is not. MyClass2 works fine (when I define NOTEMPLATED so MyClass is not compiled). But when I compile with MyClass, I get the following error:
If I remove the triangularView<Lower>() part, I would then get a message saying that there is no member solve(). I'm really new to templates (and C++, for that matter), so I'm not sure what to do here. Note that I have tried to follow, as best I can, the instructions on the "Writing Functions that Take Eigen Types as Parameters" help page. But that also led to a second question. In MyClass, there is only one template parameter, Derived. In the example for storing the result on the help page, there is another template parameter, OtherDerived. The examples on the page seem to address only templated functions, but not functions that are members of templated classes. So what would I do in this case? Thanks in advance for your help with this, and for creating the library. I am still brand new to Eigen, but I am excited to use it. Michael |
Moderator
|
you have to use the C++ template keyword:
out = A.template triangularView<Lower>().transpose().solve(x); It works as the typename keywords (e.g., typename Derived::Scalar) but for members rather than types. |
Registered Member
|
Thank you, that did the trick. I have never seen the template keyword used in that way before, and a search through various C++ resources (both online and offline) yielded nothing as well. Can you point me towards a general explanation of when the .template keyword is needed? And might I suggest adding this to the tutorial?
Thanks again. |
Registered Member
|
The best explanation is in the Appendix B ("The typename and template Keywords") of the "C++ Template Metaprogramming" by David Abrahams, Aleksey Gurtovoy. If you don't have it handy, you can look at the following explanations: http://pages.cs.wisc.edu/~driscoll/typename.html http://www.parashift.com/c++-faq-lite/t ... #faq-35.18 http://www.comeaucomputing.com/techtalk ... lateprefix http://www.comeaucomputing.com/techtalk ... /#typename |
Registered Member
|
Thanks. Those are helpful references.
|
Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]