如何根据传递给函数的变量定义特征矩阵大小
How to Define an Eigen Matrix size based upon a variable passed to a function
我正在尝试编写需要根据输入定义矩阵大小的代码。显示问题的精简版代码是:
#include <iostream>
#include "eigen/Eigen/Dense"
#include <cmath>
using namespace Eigen;
void matrixname( const int numbRow, const int numbcol);
int main()
{
const int numbRow=5;
const int numbCol=3;
matrixname(numbRow,numbCol);
return 0;
}
void matrixname( const int numbRow, const int numbCol)
{
Matrix<double,numbRow,numbCol> y;
}
尝试编译代码时,将返回以下错误:
/main.cpp:20:15:错误:非类型模板参数不是常量表达式
构建在尝试定义 y 的最后一行中断。
有什么方法可以修改变量的声明或传递,以便能够以这种方式定义矩阵的大小?
根据文档,如果您在编译时不知道矩阵的大小,则需要使用矩阵大小模板参数作为Eigen::Dynamic
。
因此,您可能需要按如下方式修改函数:
void matrixname( const int numbRow, const int numbCol)
{
Matrix<double,Eigen::Dynamic,Eigen::Dynamic> y1(numbRow, numbCol);
// Eigen also provides a typedef for this type
MatrixXd y2(numbRow, numbCol);
}
相关文章:
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 根据C++标准的定义实现"is_similar"类型特征
- 具有相同特征的两个对象是否只在内存中存储一次?无论定义它们的函数是什么,都是不同的
- 特征中的自定义标量类型
- 根据类型特征更改函数定义?
- 使用 C++17 编译特征需要_SILENCE_CXX17_NEGATORS_DEPRECATION_WARNING定义
- 如何将自定义特征张量类存储到 std::vector 中?
- std::begin-类型特征中未考虑用户定义的重载
- 定义模板化结构的特征时出现不完整的类型错误
- 如何根据传递给函数的变量定义特征矩阵大小
- 如何通过opencv中的程序参数定义特征类型?
- 使用 c++ 中的特征库定义全局向量
- 我可以将特征定义的变量与函数adjacent_difference一起使用吗?
- 在特征源中是为特定矩阵定义的 sum() 函数
- 在函数定义中使用特征库时,小数点不会按预期存储
- 使用std ::迭代特征和自动定义函数声明的函数
- 类型特征检查 CRTP 派生,在基类中,问题是未定义的类型
- 特征自定义类和函数参数
- c++:将特征定义的矩阵传递给函数,并使用它们 - 最佳实践
- 使用特征定义动态矩阵