c++ - 模板化构造函数中的奇怪错误

标签 c++ templates eigen eigen3

<分区>

尝试在模板化构造函数中转换参数时出现奇怪的编译器错误。这是一个最小的例子:

#include <Eigen/Dense>

class Plane
{
public:
    template <typename TVector>
    Plane(const TVector& coefficients) {
        coefficients.cast<double>(); // compiler error on this line
    }

// This compiles fine
//    Plane(const Eigen::Vector3d& coefficients) {
//        coefficients.cast<double>();
//    }
};

int main(){return 0;}

错误是:

expected primary-expression before 'double'
expected ';' before 'double'

因为这个类从来没有被实例化(main() 是空的),我认为编译器根本不会看到函数模板,所以我很困惑怎么会有这个表达式有错误吗?

最佳答案

你必须使用 template 关键字:

template <typename TVector>
Plane(const TVector& coefficients) {
    coefficients.template cast<double>();
}

关于c++ - 模板化构造函数中的奇怪错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35349673/

相关文章:

c++ - 对象的 vector 搜索 future 对象的用途?

c++ - 将 argc 和 argv 传递给 QApplication 进入单元测试用例方法

templates - 创建可通过 PrestaShop 中的所有模板访问的变量

c++ - 警告 C4661 :no suitable definition provided for explicit template instantiation request

c++ - Eigen - 将 vector reshape 为矩阵

c++ - 如何从 Eigen::Matrix 获取内存所有权?

c++ - 使用特定范围内的随机数初始化特征矩阵或 vector 的有效方法?

c++ - 区分注释代码与有效注释

c++ - CUDA 缩减 - 竞争条件?

c++模板参数不明确