简体   繁体   English

在类中初始化模板对象(C ++)

[英]Initialize a template object within a class (c++)

I want to denfine a class based on the Eigen library: 我想定义一个基于Eigen库的类:

header file: 头文件:

#include <Eigen>

using namespace Eigen;

class MatrixV{
    public:
        MatrixV(double mu, double omega, double delta, double size);
        Eigen::MatrixXd getV();
    private:
        Eigen::MatrixXd V;
        Eigen::JacobiSVD<Eigen::MatrixXd, Eigen::NoQRPreconditioner> svd(V, ComputeFullU | ComputeFullV);
};

cpp-file: CPP文件:

MatrixV::MatrixV(double mu, double omega, double delta, double size){
    Eigen::MatrixXd V = MatrixXd::Random(3,3)
}

Eigen::MatrixXd MatrixV::getV(){
    return V;
}

By compiling that code, my compiler gives me the following errors: 通过编译该代码,我的编译器给了我以下错误:

MatrixV.h:14:68: error: 'V' is not a type
   Eigen::JacobiSVD<Eigen::MatrixXd, Eigen::NoQRPreconditioner> svd(V,ComputeFullU | ComputeFullV);

MatrixV.h:14:71: error: 'ComputeFullU' is not a type
   Eigen::JacobiSVD<Eigen::MatrixXd, Eigen::NoQRPreconditioner> svd(V, ComputeFullU | ComputeFullV);

MatrixV.h:14:84: error: expected ',' or '...' before '|' token
   Eigen::JacobiSVD<Eigen::MatrixXd, Eigen::NoQRPreconditioner> svd(V, ComputeFullU | ComputeFullV);
                                                                              So the main problem seems to be the line 
Eigen::JacobiSVD<Eigen::MatrixXd, Eigen::NoQRPreconditioner> svd(V, ComputeFullU | ComputeFullV);

but I just copy-pasted it from http://eigen.tuxfamily.org/dox/classEigen_1_1JacobiSVD.html and I also don't understand why he expects type names in the ()-brackets anyway. 但是我只是从http://eigen.tuxfamily.org/dox/classEigen_1_1JacobiSVD.html复制粘贴了它,而且我也不明白为什么他仍然希望()括号中的类型名称。 Do you have any Ideas? 你有什么想法? Thanks a lot! 非常感谢!

Your error messages: 您的错误消息:

MatrixV.h:14:68: error: 'V' is not a type
   Eigen::JacobiSVD<Eigen::MatrixXd, Eigen::NoQRPreconditioner> svd(V,ComputeFullU | ComputeFullV);

MatrixV.h:14:71: error: 'ComputeFullU' is not a type
   Eigen::JacobiSVD<Eigen::MatrixXd, Eigen::NoQRPreconditioner> svd(V, ComputeFullU | ComputeFullV);

MatrixV.h:14:84: error: expected ',' or '...' before '|' token
   Eigen::JacobiSVD<Eigen::MatrixXd, Eigen::NoQRPreconditioner> svd(V, ComputeFullU | ComputeFullV);

are all caused by the same issue. 都是由同一问题引起的。 The compiler thinks you're declaring a function, but it looks like you want to declare a variable svd . 编译器认为您正在声明一个函数,但是看起来您想声明一个变量svd What you should do is to remove the parenthesis, and move it to the constructor: 您应该做的是删除括号,并将其移到构造函数中:

#include <Eigen>

using namespace Eigen;

class MatrixV{
    public:
        MatrixV(double mu, double omega, double delta, double size);
        Eigen::MatrixXd getV();
    private:
        Eigen::MatrixXd V;
        Eigen::JacobiSVD<Eigen::MatrixXd, Eigen::NoQRPreconditioner> svd;
};

And change the constructor: 并更改构造函数:

MatrixV::MatrixV(double mu, double omega, double delta, double size) {
    V = MatrixXd::Random(3,3)
    svd.compute(V, ComputeFullU | ComputeFullV);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM