Reputation: 22173
I'm trying to extend the Ref Eigen
class in order to use custom classes. I've got the following code:
#include <iostream>
#include <eigen3/Eigen/Dense>
class Interface {
public:
virtual ~Interface() {
}
virtual void customMethod() const = 0;
};
class MyVectorType: public Eigen::Matrix<double, 3, 1, Eigen::DontAlign>,
public Interface {
public:
MyVectorType(void) :
Eigen::Matrix<double, 3, 1, Eigen::DontAlign>() {
}
typedef Eigen::Matrix<double, 3, 1, Eigen::DontAlign> Base;
// This constructor allows you to construct MyVectorType from Eigen expressions
template<typename OtherDerived>
MyVectorType(const Eigen::MatrixBase<OtherDerived>& other) :
Eigen::Matrix<double, 3, 1, Eigen::DontAlign>(other) {
}
// This method allows you to assign Eigen expressions to MyVectorType
template<typename OtherDerived>
MyVectorType & operator=(const Eigen::MatrixBase<OtherDerived>& other) {
this->Base::operator=(other);
return *this;
}
virtual void customMethod() const {
std::cout << rows() << std::endl;
}
};
template<typename T, int Options>
class MyRef: public Eigen::Ref<typename T::Base, Options, Eigen::Stride<0, 0> >,
public Interface {
public:
typedef Eigen::Ref<typename T::Base, Options, Eigen::Stride<0, 0> > Base;
template<typename Derived>
MyRef(Eigen::DenseBase<Derived>& expr) :
Eigen::Ref<typename T::Base, Options, Eigen::Stride<0, 0> >(expr) {
}
virtual void customMethod() const {
std::cout << rows() << std::endl; // <-----error
}
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(MyRef)};
template<typename T, int Options>
class MyRef<const T, Options> : public Eigen::Ref<typename T::Base, Options,
Eigen::Stride<0, 0> >, public Interface {
public:
template<typename Derived>
MyRef(const Eigen::DenseBase<Derived>& expr) :
Eigen::Ref<typename T::Base, Options, Eigen::Stride<0, 0> >(expr) {
}
virtual void customMethod() const {
std::cout << rows() << std::endl; // <-----error
}
};
void init(MyRef<MyVectorType, Eigen::Unaligned> m) {
m.customMethod();
}
int main() {
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::AutoAlign, 12,
12> mm(3, 1);
Eigen::Map<MyVectorType::Base> map(mm.data(), 3, 1);
MyRef<MyVectorType, Eigen::Unaligned> ref(map);
init(ref);
std::cout << mm << std::endl;
return 0;
}
In order to call custom methods like the method init()
, the same interface must be used between MyVectorType
and MyRef
. So I thought to use an Interface
class.
The problem: This code doesn't compile because I can't call rows()
inside MyRef
, so I don't understand how to access to MyVectorType
or the underlying data in Ref
class to call other methods.
I tried with derived()
to access but it doesn't work. I looked at the source code but I don't understand how Ref
can be used normally with all interface of DenseBase
. I'd like to do the same thing for my custom methods.
Gcc error:
../main.cpp:49:16: error: there are no arguments to ‘rows’ that depend on a template parameter, so a declaration of ‘rows’ must be available [-fpermissive]
std::cout << rows() << std::endl;
^~~~
../main.cpp:49:16: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
../main.cpp: In member function ‘virtual void MyRef<const T, Options>::customMethod() const’:
../main.cpp:63:16: error: there are no arguments to ‘rows’ that depend on a template parameter, so a declaration of ‘rows’ must be available [-fpermissive]
std::cout << rows() << std::endl;
^~~~
Upvotes: 4
Views: 271
Reputation: 32732
When a Base
class depends upon the template parameters, even though Derived
(let's say) member_base
inherited from the Base
, using simply member_base
in Derived
class, is not equivalent to this->member_base
.
That is
template<typename T>
class Base { public: void member_base(); };
template<typename T>
class Derived : Base<T>
{
public:
void member_derived()
{
member_base(); // calls external(global) member_base() or error
}
};
In your case, what happend to rows()
is exactly same as above case.
You need to qualify using either this->
or Base<T>::
, for all members what you inherited from Base
.
In your case
this->row()
or
Eigen::Ref<typename T::Base, Options, Eigen::Stride<0, 0> >::rows()
Upvotes: 3