C ++:使用Eigen时将函数传递给构造函数(C++: passing a function to a constructor when using Eigen)

编程入门 行业动态 更新时间:2024-10-28 21:25:52
C ++:使用Eigen时将函数传递给构造函数(C++: passing a function to a constructor when using Eigen)

我在一个正在研究的课堂上使用Eigen。 代码摘录如下:

class model { public: Eigen::MatrixXd Ac; model (vars &, std::function<Eigen::MatrixXd()>, std::function<Eigen::MatrixXd()>); }; model::model (vars & vars, std::function<Eigen::MatrixXd()> muX, std::function<Eigen::MatrixXd()> muX2) //update Ac Ac = muX(vars) + muX2(vars); };

我们的想法是,在创建model对象时,用户必须提供muX和muX2函数,类将使用muX和muX2填充MatrixXd Ac中的值。 vars只是一个由我创建的多个变量组成的类,而muX和muX2应该使用vars的数据作为输入。

但是,在我的XCode中,它给了我一个错误说:“没有匹配函数来调用`std :: function类型的对象”。 我还没有编译,但我认为编译器会给我同样的错误。

I'm using Eigen in a class that I'm working on. Here's the code excerpt:

class model { public: Eigen::MatrixXd Ac; model (vars &, std::function<Eigen::MatrixXd()>, std::function<Eigen::MatrixXd()>); }; model::model (vars & vars, std::function<Eigen::MatrixXd()> muX, std::function<Eigen::MatrixXd()> muX2) //update Ac Ac = muX(vars) + muX2(vars); };

The idea is that when creating an object of model, the user will have to supply the functions muX and muX2, and the class will fill in values in MatrixXd Ac using muX and muX2. vars is just a class consisting of multiple variables that I created, and muX and muX2 are expected to use the data in vars as input.

However, in my XCode, it's giving me an error saying: "No matching function for call to object of type `std::function". I haven't compiled yet but I think the compiler will give me the same error.

最满意答案

std::function<Eigen::MatrixXd()>意味着它接受一个不接受任何参数的函数并返回一个Eigen::MatrixXd ,我认为这不是你想要做的。 您可能想将其更改为std::function<Eigen::MatrixXd(vars&)>

std::function<Eigen::MatrixXd()> implies that it takes a function which accepts no arguments and returns an Eigen::MatrixXd, which I think is not what you want to do. You probably want to change it to std::function<Eigen::MatrixXd(vars&)>

更多推荐

本文发布于:2023-07-17 04:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1139211.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   时将   Eigen   constructor   function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!