从模板化类创建对象时出错 [英] Error while creating object from templated class

查看:50
本文介绍了从模板化类创建对象时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试寻找一种从C ++中的多元正态分布中采样随机向量的方法,该方法既具有均值向量又具有协方差矩阵,就像Matlab的 mvnrnd 函数一样.我在

I've been trying to find a way to sample random vectors from a multivariate normal distribution in C++, having both the mean vector and the covariance matrix, much like Matlab's mvnrnd function works. I've found relevant code for a class that implements this on this page, but I've been having some problems compiling it. I've created a header file that is being included on my main.cpp, and I'm trying to create an object of the EigenMultivariateNormal class:

MatrixXd MN(10,1);
MatrixXd CVM(10,10);

EigenMultivariateNormal <double,int> (&MN,&CVM) mvn;

问题是我在编译时收到与模板相关的错误:

The problem is I get a template-related error when compiling:

error: type/value mismatch at argument 2 in template parameter list for ‘template<class _Scalar, int _size> class EigenMultivariateNormal’    
error:   expected a constant of type ‘int’, got ‘int’    
error: expected ‘;’ before ‘mvn’

我对如何使用模板只有一个肤浅的想法,而我绝不是cpp专家,所以我想知道我到底在做什么错?显然我应该在代码中的某个地方有一个 const .

I only have a superficial idea on how to work with templates, and I am by no means a cpp expert, so I was wondering what exactly am I doing wrong? Apparently I should have a const somewhere on my code.

推荐答案

template< class _Scalar,int _size>EigenMultivariateNormal 类是专门的模板类.第一个 class _Scalar 要求输入类型,而 int _size 要求输入整数.

template<class _Scalar, int _size> class EigenMultivariateNormal is specialized template class. The first class _Scalar ask for a type but int _size ask for an int.

您应该使用常量int而不是像通常那样的int来调用它.其次,您实例化新类EigenMultivariateNormal的语法是错误的.尝试以下方法:

You should call it with a constant int instead of the type int as you did. Secondly, your syntax to instance a new class EigenMultivariateNormal is wrong. Try this instead:

EigenMultivariateNormal<double, 10> mvn (&MN, &CVM); // with 10 is the size

这篇关于从模板化类创建对象时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆