问题标签 [mem-fun]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
985 浏览

c++ - 成员变量指针

对于给定的结构:

我可以为函数创建一个调用 wapper:std::mem_fn( &foo::fooFunc )这样我就可以将它传递给另一个方法并在一个对象上调用它。
我想知道是否有类似的调用包装器,但用于成员变量。

例如,我在这里使用指向成员变量的指针,但我想使用调用包装器:

0 投票
0 回答
678 浏览

c++ - 使用 std::mem_fn 调用 std::function 类成员

我的计划是构建几个拥有预定义“回调挂钩”的监听器类。

在下面的示例中,类Foo有一个名为onChange. 它将在构造过程中设置为默认回调函数。它也可以设置为提供正确接口的任意函数,如 objectf1和 function所示callback()

问题是当我想onChange在 for 循环中调用对象成员时,编译器说我提供了很多参数。我知道我没有提供成员函数std::mem_fn而是提供一个对象成员的问题,它是一个函数包装器。

我如何设法将参数传递给std::function类的成员对象,Foo而不使用std::bind和使用std::mem_fn,如示例中所示?

0 投票
1 回答
138 浏览

c++ - 我必须使用 boost::bind 来创建多态“转换”功能吗?

我正在尝试使用指定的参数对向量中的每个对象调用成员函数,并且我希望调用是多态的。我相信下面显示的函数 vstuff 可以实现这一点。但是可以修改 vstuff 以在不使用 boost::bind 的情况下采用向量<shared_ptr <Base>>?

我知道 shared_ptr 需要 mem_fn 而不是 mem_fun ,但是我没有成功让 mem_fn 与我需要传入参数 t 的 bind2nd 一起工作,所以我想知道它是否可行..?

0 投票
2 回答
774 浏览

c++ - 如果元素存在于对向量中,则在类中查找

我正在用 C++ 编写代码。我有一个包含这么多文件的项目。我有一个名为 list 的对向量,如下所示:

我想检查一个特定的双精度值z是否存在一个元素:el在列表中,这样: el.second == z

我想用find_if

为此,我实现了一个方法:Scheduled它接受两个参数:第一个是存储在列表中的元素,第二个是要查找的特定值。我尝试了几种方法,但最终总是以第一种方式出现错误:

在另一个方法中但仍在同一个类中:classA

此解决方案给出以下错误:

第二种方式:直接使用 lambda

z 是 classA 的成员变量第二种编码方式会导致此错误:

'find_if(std::vector >::iterator, std::vector >::iterator, classA::method1(int)::__lambda0)'</p>

0 投票
2 回答
571 浏览

c++ - 连接信号时gtkmm编译器错误

我正在开发一个带有 GUI 的应用程序,但在尝试在连接处为另一个信号(signal_changed,来自 Gtk::ComboBox)发出信号(sig_showList,来自 View)时遇到问题,我真的很感激你帮助。代码看起来像这样:

“查看.h”

“查看.c”

我用g++ -std=c++98 *.cpp -o out $(pkg-config gtkmm-3.0 --cflags --libs). 我得到的错误是:

[实际类称为“Vista”]

好吧,我不知道那是什么意思!此外,这已经发生了,不知何故我设法解决了它,但经过一些重新格式化和重新设计后,它又出现了,我不知道该怎么办。它肯定与 combo->signal_clicked().connect(...) 行有关,因为当我删除它时编译正常。

0 投票
2 回答
179 浏览

c++ - C++: Creating a function object with mem_fn and bind1st

Disclaimer: This description contains a lot of Qt specific functionality. This is not necessary to answer the question, I'm just including it to explain the background.

I need to do some heavy computations in my QT application. In order to do this, I would like to use QtConcurrent::run(myFunction) This is Qt's version of async and creates a future, which at some point will contain the result of myFunction.

The problem is that the function is both a member function and takes complex parameters.

I know that you can pass both a function and a pointer to QtConcurrent::run. The function will then be invoked on the pointer. You can even provide a list of parameters. But it seems like this list only accepts parameters such as int, double or QString.

Actual Question:

I would like to convert this line of code:

into

That means I need to

  1. bind the pointer to the function
  2. bind the arguments to the function

This is my code so far:

Unfortunately this doesn't work. There are 18 compiler errors, here is the pastebin: http://pastebin.com/2rBQgFNL

It would be great, if you could explain how to do this properly. Not necessary for an answer, but even better, would be code for QtConcurrent::run.

0 投票
1 回答
325 浏览

c++ - void 麻烦将值返回到字符串

根据这篇文章,我尝试创建一个通用解析器,它将被调用函数的返回值作为字符串输出。

将 std::strings 的 std::vector 解析为任意类型的 std::tuple

不幸的是,我没有弄清楚如何格式化返回 void 的函数的返回值......

和:

一类:

主要的:

编译器调用:

在 R 不是 void 类型的 RT::f() 上调用 Execute 时出现错误消息 (1):

在 void T::f(Args...) 上调用 Execute 时出现错误消息 (2),其中 Args... 是非 void:

0 投票
1 回答
129 浏览

c++ - 抛出模板特化错误的函数指针数组(包括成员函数)

所以,我有一个名为 Delegate 的类,它可以存储一个函数指针数组。这是代码:

Player.cpp 中的用法:

这会引发错误 C2893(错误 C2893 无法专门化函数模板 'unknown-type std::invoke(_Callable &&,_Types &&...)')

但是当我将 Bind 的定义更改为以下内容时:

它工作正常,但是当我尝试将函数对象推入向量时,它再次引发相同的错误。

有没有办法解决这个问题?

我需要缓存传入的指针。

0 投票
2 回答
182 浏览

c++ - 为什么我不能在绑定中使用 mem_fn Functor?

我想传递一个mem_fn参数,bind但编译器似乎不允许它。

例如,这很好用:

但是当我尝试使用mem_fn仿函数时,我得到了一页错误:

/usr/include/c++/6/bits/stl_numeric.h:在 '_Tp std::accumulate(_InputIterator, _InputIterator, _Tp, _BinaryOperation) 的实例化中 [with _InputIterator = __gnu_cxx::__normal_iterator >; _Tp = int; _BinaryOperation = std::_Bind(std::_Placeholder<1>, std::_Mem_fn)>]':
prog.cpp:20:102: 这里需要
/usr/include/c++/6/bits/stl_numeric.h: 154:22:错误:不匹配调用 '(std::_Bind(std::_Placeholder<1>, std::_Mem_fn)>) (int&, foo* const&)'</p>