【发布时间】:2013-11-11 04:34:51
【问题描述】:
我下载了 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);
“EigenTest.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;"
【问题讨论】:
-
什么是
Type1?您在问题中显示的程序不是产生错误的程序。请出示真正的程序。 -
很抱歉在他最初的帖子中遗漏了部分代码。 Type1 实际上是“模板类型名”
-
不过,简而言之,看起来来自调用
matrix(0, 0)的返回(我不太明白,但还早)与Type1的类型不匹配 - 不管是什么,你还没有向我们展示Type1的定义,大概在某个 typedef 的某个地方。 -
我建议你了解SSCCE 是什么。
-
伙计们,代码丢失了
<>,我现在收到了OP描述的错误。
标签: c++ templates compiler-errors eigen eigen3