使用模板化特征矩阵初始化
class using templated eigen matrix
我写了一个类
class abc
{
public:
template <typename Derived >
abc( const Eigen::MatrixBase < Derived > &matA,
const Eigen::MatrixBase < Derived > &matB,
Eigen::MatrixBase < Derived > &matC );
};
template <typename Derived >
abc::abc( const Eigen::MatrixBase < Derived > &matA,
const Eigen::MatrixBase < Derived > &matB,
Eigen::MatrixBase < Derived > &matC )
{
matC.derived().resize( matA.rows(), matA.cols() );
for( int r = 0; r < matA.rows(); r++ )
{
for( int c = 0; c < matA.cols(); c++ )
{
matC(r,c) = log( matA(r,c) )/log( matB(r,c) );
}
}
}
,但在main中使用类ABC时我得到未定义的引用错误
typedef Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic > Matrix_Float;
main()
{
Matrix_Float matA, matB, matC;
// put values in matA, matB
abc cls_abc( matA, matB, matC );
}
错误错误:对' abc::abc <特征:矩阵&>>(特征:MatrixBase & lt;特征:矩阵& lt;float, -1, -1, 0, -1, -1>> constent &, Eigen::MatrixBase <特征:矩阵&>> constent &, Eigen::MatrixBase <特征:矩阵&>> &)'
类定义的语法有什么问题吗?
请帮助。
你有几个问题。首先,对于模板类,您需要在声明中提供实现(即在.h
文件中,而不是在.cpp
文件中)。其次,您希望类被模板化,请注意构造函数。第三,当使用模板化类时,需要指定模板形参。
.h
)文件应该像这样:
template <typename Derived > class abc // Template the class
{
public:
//template <class Derived > // Don't put this here, but above
abc(
const Eigen::MatrixBase < Derived > &matA,
const Eigen::MatrixBase < Derived > &matB,
Eigen::MatrixBase < Derived > &matC);
};
template <typename Derived >
abc<Derived>::abc(
const Eigen::MatrixBase < Derived > &matA,
const Eigen::MatrixBase < Derived > &matB,
Eigen::MatrixBase < Derived > &matC)
{
matC.derived().resize(matA.rows(), matA.cols());
for (int r = 0; r < matA.rows(); r++)
{
for (int c = 0; c < matA.cols(); c++)
{
matC(r, c) = log(matA(r, c)) / log(matB(r, c));
}
}
}
和您的main()
应该看起来像:
int main(int argc, char* argv[]) // just "main()" is so passé
{
Matrix_Float matA, matB, matC;
// put values in matA, matB
abc<Matrix_Float> cls_abc(matA, matB, matC);
}
相关文章:
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 特征::向量;在函数中使用 Eigen::Matrix3f 的值初始化向量,大于 4 个条目
- 逗号初始化和构造函数C++和特征
- 特征库中标准偏差的逐列初始化和计算
- 特征中静态成员的初始化
- 具有从特定范围的随机数初始化特征矩阵或向量初始化特征矩阵或向量的有效方法
- 在特征中初始化矩阵
- 在特征中初始化矩阵
- 特征库 --> 使用文件或现有 std::vector<string> content (c++) 中的数据初始化矩阵
- 如何初始化具有类型特征的类模板的静态数据成员
- 如何初始化类型特征的静态 std::unordered_map
- 如何从opencv cv::Mat或行主数组初始化特征矩阵
- 初始化头文件中的常量特征矩阵
- 特征库加速矩阵初始化
- 是否可以初始化一个const特征矩阵?
- 尝试为特征矩阵实现一种初始化列表语法
- 如何在c++中正确初始化、赋值和使用动态特征矩阵作为类成员
- 使用 "comma initialization" 初始化静态特征矩阵
- 使用模板化特征矩阵初始化
- 特征:逗号初始化动态矩阵