c++ - 初始化类中的模板对象 (c++)

标签 c++ class templates eigen

我想定义一个基于 Eigen 库的类:

头文件:

#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 文件:

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

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

通过编译该代码,我的编译器给出以下错误:

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);

但我只是从 http://eigen.tuxfamily.org/dox/classEigen_1_1JacobiSVD.html 复制粘贴它而且我也不明白为什么他希望在 () 括号中输入类型名称。你有什么想法?非常感谢!

最佳答案

您的错误消息:

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);

都是由同一问题引起的。编译器认为您正在声明一个函数,但看起来您想声明一个变量svd。你应该做的是删除括号,并将其移至构造函数:

#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;
};

并更改构造函数:

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

关于c++ - 初始化类中的模板对象 (c++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45881644/

相关文章:

html - 悬停某个图像时显示某些信息

python - Django-CMS 模板 block

python - Django:嵌套的自定义模板标签

c++ - 概括具有不同相似类型的 C++ 代码的方法

c++ - 如果我想专用于多种字符串,是否需要多个模板特化?

c++ - 全局结构分配给出名称类型错误

c++ - 如何使用 std::for_each?

Java:为什么我的变量没有传递给我的方法可用的构造函数?

c++ - 检查模板中 nullptr 的函数指针以获取任何类型的可调用

Python 如何扩展 `str` 并重载其构造函数?