Qt使用Eigen库时用qDebug()<<输出矩阵
qDebug无法直接输出Matrix,会报错
Eigen::Matrix<double ,1,2> a;
a<<1,2;
qDebug()<<a;//报错
可以添加头文件解决问题
#include “eigenqdebug.h”
#ifndef EIGENQDEBUG_H
#define EIGENQDEBUG_H
#include <QDebug>
#include <Eigen/Dense>
//便于使用qDebug()<<输出eigen库结果
template <typename Derived>
QDebug operator<<(QDebug debug, const Eigen::MatrixBase<Derived> &matrix){
debug.nospace() << "[";
for (int i = 0; i < matrix.rows(); ++i) {
if (i != 0)
debug.nospace() << " ";
for (int j = 0; j < matrix.cols(); ++j) {
debug.nospace() << matrix(i, j);
if (j != matrix.cols() - 1)
debug.nospace() << ", ";
}
if (i != matrix.rows() - 1)
debug.nospace() << ";\n";
}
debug.nospace() << "]";
return debug.space();
};
#endif // EIGENQDEBUG_H
输出结果如下:
[1, 2]