there seems to be a problem with the following code. I get the error message
error: expected unqualified-id before numeric constant
Eigen::Matrix M_inv1_abc = pose_l.block<3, 3>(0,
0).inverse();
This is a code sample:
template<typename T>
Eigen::Matrix<T, 4, 1> Function(Eigen::Matrix<T, 3, 4> pose_l)
{
// fails here
Eigen::Matrix<T, 3, 3> M_inv1 = pose_l.block<3, 3>(0, 0).inverse();
// this works, sample is from https://eigen.tuxfamily.org/dox/group__TutorialMatrixClass.html
Eigen::MatrixXf m(4,4);
Eigen::MatrixXf y(2,2);
m << 1, 2, 3, 4,
5, 6, 7, 8,
9,10,11,12,
13,14,15,16;
y = m.block<2,2>(1,1);
}
With the sample MatrixXf I don't use my template...
I renamed pose_l and M_inv1; in other posts, like
Expected unqualified-id before numeric constant for defining a number
a redefinition helped, but not in my case.
What am I missing?
Best
ManuKlause
pose_l.template block<3, 3>(0, 0).inverse();
For detail, you can refer to how c++ deduce the type of variables
Try this (adding parentheses around the block sub-expression):
template<typename T>
Eigen::Matrix<T, 4, 1> Function(Eigen::Matrix<T, 3, 4> pose_l)
{
Eigen::Matrix<T, 3, 3> M_inv1 = (pose_l.block<3, 3>(0, 0)).inverse();
// ...
}
Related
I passed an nested braced-init-lists to class constructor:
#include <iostream>
#include <type_traits>
#include <variant>
#include <vector>
class NestedInteger final {
public:
NestedInteger(int i) : type_(INT), val_int_(i) {}
// NestedInteger(std::initializer_list<NestedInteger> ni) {
template <typename T>
NestedInteger(std::initializer_list<T> ni) {
for (auto it = ni.begin(); it != ni.end(); it++) {
val_vec_.push_back(*it);
}
}
private:
enum { INT, VECTOR } type_;
int val_int_;
std::vector<NestedInteger> val_vec_;
};
int main() {
NestedInteger ni1{1};
NestedInteger ni2{1, 2, 3};
NestedInteger ni3{{1}, 2, 3};
NestedInteger ni4{{1, 2, 3}};
NestedInteger ni5{{1, 2, 3}, {4, 5, 6, 7}};
return 0;
}
And I got compilation error:
t.cpp:29:44: error: no matching function for call to ‘NestedInteger::NestedInteger(<brace-enclosed initializer list>)’
29 | NestedInteger ni5{{1, 2, 3}, {4, 5, 6, 7}};
| ^
t.cpp:12:3: note: candidate: ‘template<class T> NestedInteger::NestedInteger(std::initializer_list<_Tp>)’
12 | NestedInteger(std::initializer_list<T> ni) {
| ^~~~~~~~~~~~~
t.cpp:12:3: note: template argument deduction/substitution failed:
t.cpp:29:44: note: candidate expects 1 argument, 2 provided
29 | NestedInteger ni5{{1, 2, 3}, {4, 5, 6, 7}};
...
I read the list initialization in cppreference here. And I can tell the expression belongs to the first syntax type:
T object { arg1, arg2, ... }; (1)
initialization of a named variable with a braced-init-list (that is, a possibly empty brace-enclosed list of expressions or nested braced-init-lists)
I can't understand the explanation about the effects.
Could you help to figure out:
Which item in the explanation does this case match?
Is this a non-deduced contexts described here?
By the way, if I change the template to specific type as the commented line, the compilation error disapeared.
I am using Eigen, and am currently trying to write a function to operate on rows of a matrix. I've followed the guidelines in the docs but nothing I try compiles (with either clang or g++); I'm at my wits end. How should one actually write functions which will take a RowXpr?
For reference, here is what I have tried so far:
#include <iostream>
#include <Eigen/Core>
using namespace std;
using namespace Eigen;
constexpr Eigen::StorageOptions Order = ColMajor;
using vect_t = Matrix<double, 1, 3, Order>;
using matr_t = Matrix<double, Dynamic, 3, Order>;
#define FUNC 3
#if FUNC == 1
vect_t func(const vect_t& f)
{
return f;
}
#elif FUNC == 2
vect_t func(const Ref<vect_t>& f)
{
return f;
}
#elif FUNC == 3
template<class D> vect_t func(const MatrixBase<D>& f)
{
return f;
}
#endif
int main()
{
matr_t M = matr_t::Random(5,3);
cout << M << endl;
cout << func( M.row(2) ) << endl;
return 0;
}
Thanks!
Edit:
With clang (version 3.8.0-2ubuntu4) the error I get is as below. The error is comparable with g++.
dan#dan-laptop:~/workspace/scratch$ clang++ eigen_func_test.cpp -I /home/dan/Downloads/eigen_3.3.3/ --std=c++11 && ./a.out
In file included from eigen_func_test.cpp:2:
In file included from /home/dan/Downloads/eigen_3.3.3/Eigen/Core:436:
/home/dan/Downloads/eigen_3.3.3/Eigen/src/Core/PlainObjectBase.h:899:7: error: static_assert failed
"INVALID_MATRIX_TEMPLATE_PARAMETERS"
...EIGEN_STATIC_ASSERT((EIGEN_IMPLIES(MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1, (Options&RowMajor)==RowMajor)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/dan/Downloads/eigen_3.3.3/Eigen/src/Core/util/StaticAssert.h:32:40: note: expanded from macro
'EIGEN_STATIC_ASSERT'
#define EIGEN_STATIC_ASSERT(X,MSG) static_assert(X,#MSG);
^ ~
/home/dan/Downloads/eigen_3.3.3/Eigen/src/Core/PlainObjectBase.h:535:7: note: in instantiation of member function
'Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 0, 1, 3> >::_check_template_params' requested here
_check_template_params();
^
/home/dan/Downloads/eigen_3.3.3/Eigen/src/Core/Matrix.h:379:9: note: in instantiation of function template
specialization 'Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 0, 1, 3>
>::PlainObjectBase<Eigen::Block<Eigen::Matrix<double, -1, 3, 0, -1, 3>, 1, 3, false> >' requested here
: Base(other.derived())
^
eigen_func_test.cpp:32:9: note: in instantiation of function template specialization 'Eigen::Matrix<double, 1, 3, 0,
1, 3>::Matrix<Eigen::Block<Eigen::Matrix<double, -1, 3, 0, -1, 3>, 1, 3, false> >' requested here
return f;
^
eigen_func_test.cpp:41:10: note: in instantiation of function template specialization
'func<Eigen::Block<Eigen::Matrix<double, -1, 3, 0, -1, 3>, 1, 3, false> >' requested here
cout << func( M.row(2) ) << endl;
^
1 error generated.
If you read the part highlighted by clang
EIGEN_STATIC_ASSERT((EIGEN_IMPLIES(MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1, (Options&RowMajor)==RowMajor)
you see that, if a matrix has one row at compile time and more than one column, it must be RowMajor. That means, if you set Order = RowMajor or just leave out the , Order in
using vect_t = Matrix<double, 1, 3, Order>;
your code should compile fine (it looks like everything else are just follow-up errors).
Not exactly sure what is causing your compiler error directly but from what you have shown in your source code as well as your compiler errors. This is the part of the compiler error message that I'm basing this off of.
/home/dan/Downloads/eigen_3.3.3/Eigen/src/Core/PlainObjectBase.h:899:7: error: static_assert failed
"INVALID_MATRIX_TEMPLATE_PARAMETERS"
So when looking at the source code you are trying to use the #define func3
that you have declared as such:
template<class D> vect_t func(const MatrixBase<D>& f) {
return f;
}
with the using directives that you have declared as:
constexpr Eigen::StorageOptions Order = ColMajor;
using vect_t = Matrix<double, 1, 3, Order>;
using matr_t = Matrix<double, Dynamic, 3, Order>;
So let's expand this to the full form to see what the compiler is trying to parse or deduce as your parameters and return type.
template<class D>
Matrix<double, 1, 3, constexpr Eigen::StorageOptions Order = ColMajor>
func( const MatrixBase<D>& f ) {
return f;
}
Then in your main you use this with:
int main() {
// matr_t M = matr_t::Random(5,3);
Matrix<double, Dynamic, 3, constexpr Eigen::StorageOptions Order = ColMajor> M
= Matrix<double, Dynamic, 3, constexpr Eigen::StorageOptions Order = ColMajor>::Random(5, 3);
// Then you call your function passing it this:
std::cout << func( M.row(2) ) << std::endl;
return 0;
}
func() is returning Matrix<double, 1, 3, Order>;
and it takes const MatrixBase<D>&
however it appears you are passing it: M.row(2)
that is constructed from: Matrix<double, Dynamic, 3, constexpr Eigen::StorageOptions Order = ColMajor>::Random(5, 3)
In the function itself you are returning f which happens to be
the function's const ref of MatrixBase<D> parameter yet the declaration is expecting to return a Matrix<double, 1, 3, Order>
So I think the compiler is having a hard time trying to convert a
Matrix<double, Dynamic, 3, Order> to a Matrix<double, 1, 3, Order>
Maybe this will help you to understand the compiler error a little better; as you can see when you look at the rest of the compilation error message you can tell that this is evident when it is trying to do the specializations of this template.
Since C++11, it is possible to initialize member variables in class definitions:
class Foo {
int i = 3;
}
I know I can initialize an std::array like this:
std::array<float, 3> phis = {1, 2, 3};
How can I do this in a class definition? The following code gives an error:
class Foo {
std::array<float, 3> phis = {1, 2, 3};
}
GCC 4.9.1:
error: array must be initialized with a brace-enclosed initializer
std::array<float, 3> phis = {1, 2, 3};
^ error: too many initializers for 'std::array<float, 3ul>'
You need one more set of braces, which is non-intuitive.
std::array<float, 3> phis = {{1, 2, 3}};
I can use enable_if to separate behavior by parameter type such as:
std::vector<int>
Now I want to separate behavior by the inner type of a container:
int of std::vector<int>
what can I do in c++?
I wonder if this is what you mean:
#include<iostream>
#include<vector>
#include<type_traits>
// The following function will display the contents of the provided T
// only if its value_type trait is of type int.
template<typename T>
typename std::enable_if<
std::is_same<typename T::value_type, int>::value,
void>::type display(const T& data) {
std::cout<<"Some ints:"<<std::endl;
for(int xi : data) {
std::cout<<xi<<" ";
}
std::cout<<std::endl;
}
int main() {
std::vector<int> dint = {1, 2, 3, 4, 5, 6};
display(dint);
std::vector<float> dfloat = {1, 2, 3, 4, 5, 6};
// The following call would lead to compile-time error, because
// there is no display function activated for float types:
//display(dfloat);
return 0;
}
Compiling with g++ example.cpp -std=c++11 -Wall -Wextra (OS X 10.7.4 using GCC 4.8.1) yields:
$ ./a.out
Some ints:
1 2 3 4 5 6
As expected, if I uncomment the display(dfloat) line the compiler error message includes:
error: no matching function for call to ‘display(std::vector<float>&)’
Something like the following, where you can fill in int, double, etc. for S?
std::vector<std::enable_if<std::is_same<T, S>::value, S>::type>
I'm trying to sum the elements of a static array in compile-time via templates:
#include <type_traits>
template<size_t idx, int* arr>
struct static_accumulate :
std::integral_constant<size_t, arr[idx] + static_accumulate<idx - 1, arr>::value>
{ };
template<int* arr>
struct static_accumulate<0, arr> : std::integral_constant<size_t, arr[0]>
{ };
constexpr int arr[9] = {1, 2, 3,
4, 5, 6,
7, 8, 9};
int main()
{
std::cout<<static_accumulate<8, arr>::value;
}
but I got this compile-error:
error: could not convert template argument ‘arr’ to ‘int*’
compiler - gcc 4.7.
How I can avoid it?
Change your template arguments to int const * arr.
Clang's error message is actually more helpful here than gcc's:
sum.cc:19:37: error: non-type template argument of type
'int const[9]' cannot be converted to
a value of type 'int *'