编译错误 将模板编程与特征C++库结合使用
Compilation Error Using template programming with Eigen C++ library
我下载了Eigen(3)库并开始使用它。我编写了一个模板函数,并在函数内声明了一个"模板类型"的局部变量。我收到以下编译错误。
$ g++ EigenTest.cpp
EigenTest.cpp: In instantiation of ‘void myFunc(Eigen::MatrixBase<Derived>&) [with Type1 = Eigen::Matrix<double, -1, -1>]’:
EigenTest.cpp:24:10: required from here
EigenTest.cpp:16:26: error: conversion from ‘Eigen::DenseCoeffsBase<Eigen::Matrix<double, -1, -1>, 1>::Scalar {aka double}’ to non-scalar type ‘Eigen::Matrix<double, -1, -1>’ requested
Type1 tmp = matrix(0, 0);
"特征测试.cpp"如下。
#include "Eigen/Dense"
#include <iostream>
template<typename Type1>
void myFunc(Eigen::MatrixBase<Type1>& matrix)
{
int i=matrix.rows();
Type1 tmp = matrix(0, 0); // getting compiler error here
std::cout<<"tmp is ->"<<tmp<<std::endl;
}
int main()
{
Eigen::MatrixXd m(2,2);
m.setConstant(100);
myFunc(m);
return 0;
}
我还尝试使用"typename Type1 tmp = matrix(0, 0);"
这也行不通!
如何解决这个问题?在正常的C++模板编程(没有 Eigen)中,我可以在模板函数中定义一个局部变量作为 'Type1 tmp;"
在Eigen::MatrixBase<Type1>
中,Type1
不是标量类型,而是实际表达式的类型。在你的例子中,它将是MatrixXd,但如果调用myFunc,例如m.block(...),那么Type1
将是一个Block<...>。若要获取标量类型,可以使用 Type1::Scalar:
template<typename Type1>
void myFunc(Eigen::MatrixBase<Type1>& matrix)
{
typename Type1::Scalar Scalar;
Scalar tmp = matrix(0, 0);
}
如果您需要类似于Type1
的矩阵类型,请使用Type1::PlainObject
,例如:
typename Type1::PlainObject mat = 2 * matrix * matrix.transpose();
看起来MatrixBase
使用"CRTP"(见这里),模板参数实际上是从它派生的类型。 因此,在你使用方法myFunc()
时,Type1
实际上是在表示Eigen::MatrixXd
,我认为你认为Type1
是一个双精度。 所以,这一行:
Type1 tmp = matrix(0, 0);
在这个库的文档中(见这里),MatrixXd
的 typedef 是一个双精度矩阵,所以我想从 matrix(0, 0)
返回是一个双精度,并且由于 tmp 是Type1
Eigen::MatrixXd
,一个不会进入另一个。
扫描文档我认为您的函数最好将Matrix
作为参数,这样标量类型应该可用。 像这样:
template<class T, int rows, int cols, int opts, int maxR, int maxC >
void myFunc( Eigen::Matrix<T, rows, cols, opts, maxR, maxC>& matrix )
{
T tmp = matrix(0, 0);
}
(虽然看起来很可怕!! ;-) )
在你的代码中,Type1
被推导为double
(因为Eigen::MatrixXd
是这样定义的)。
然后你正在尝试做
Type1 tmp = matrix(0, 0);
而且恐怕我的本征知识还不够,所以我通过 Clang 3.3 运行它,并得到这个错误:
test.cpp:9:7: error: no viable conversion from 'Scalar' (aka 'double') to
'Eigen::Matrix<double, -1, -1, 0, -1, -1>'
Type1 tmp = matrix(0, 0); // getting compiler error here
^ ~~~~~~~~~~~~
test.cpp:17:1: note: in instantiation of function template specialization
'myFunc<Eigen::Matrix<double, -1, -1, 0, -1, -1> >' requested here
myFunc(m);
^
/usr/include/eigen3/Eigen/src/Core/Matrix.h:210:5: note: candidate constructor not viable:
no known conversion from 'Scalar' (aka 'double') to
'internal::constructor_without_unaligned_array_assert' for 1st argument
Matrix(internal::constructor_without_unaligned_array_assert)
^
/usr/include/eigen3/Eigen/src/Core/Matrix.h:284:25: note: candidate constructor not
viable: no known conversion from 'Scalar' (aka 'double') to 'const
Eigen::Matrix<double, -1, -1, 0, -1, -1> &' for 1st argument
EIGEN_STRONG_INLINE Matrix(const Matrix& other)
^
/usr/include/eigen3/Eigen/src/Core/Matrix.h:272:25: note: candidate template ignored:
could not match 'MatrixBase<type-parameter-0-0>' against 'double'
EIGEN_STRONG_INLINE Matrix(const MatrixBase<OtherDerived>& other)
^
/usr/include/eigen3/Eigen/src/Core/Matrix.h:292:25: note: candidate template ignored:
could not match 'ReturnByValue<type-parameter-0-0>' against 'double'
EIGEN_STRONG_INLINE Matrix(const ReturnByValue<OtherDerived>& other)
^
/usr/include/eigen3/Eigen/src/Core/Matrix.h:303:25: note: candidate template ignored:
could not match 'EigenBase<type-parameter-0-0>' against 'double'
EIGEN_STRONG_INLINE Matrix(const EigenBase<OtherDerived> &other)
^
1 error generated.
这告诉我你不能这样称呼matrix
,用两个0作为参数。这也是奇怪的语法,因为MatrixBase
类没有您似乎试图调用的operator()
。
- 多态性和功能结合
- 将 Vulkan 与 SFML 结合使用?
- 将 c++ 生成器应用程序与外部 dll 结合使用
- 如何将增强MSM与增强信号结合使用?
- MS Visual Studio 解决方案结合了非托管C++项目和 C# 项目
- 函数未在作用域中声明 / 如何结合使用 header.h、header.cpp 和 main.cpp?
- 将预编译的 C 共享库与 JNI/NDK 结合使用
- C++ 将函数指针与最佳性能相结合
- 将 Lua 与 C++ DLL 结合使用
- 为什么push_back和emplace_back结合在一起时,会有不同的行为
- 将 aws-iot-device-sdk-cpp-v2 与 CMake 结合使用
- 将2个欧拉角与GLM相结合的问题
- 结合std::execution和OpenMP是否可取
- 抽象和派生与std::list相结合
- 想要将 CGAL 与四元数相结合是否合乎逻辑
- 存储/传递 v8 承诺解析器供以后使用的最佳实践?(结合C++线程)
- 如何将 IAudioClient3 (WASAPI) 与实时工作队列 API 结合使用
- 可变参数模板与默认模板参数相结合
- 函数与返回类型 node* 与 C++ 中的 OOP 结合使用
- 是否可以将C++与任何语言相结合?