c++ - 在 C++ 中对带参数(字符串)的函数使用 std bind

标签 c++ templates bind

我正在尝试在我的记录器类中创建一个 std::functions vector 。当我尝试将方法绑定(bind)到我的 std::function 时:

NcursesWindow log_win("Logs",LINES-1,COLS/3,0,COLS*2/3);
std::function<void(std::string)> f = std::bind(&NcursesWindow::add_string,&log_win);

add_string 函数定义如下:

void add_string(string text);

但是,gcc(使用 gfilt 插件来尝试理解模板错误)返回:

BD Software STL Message Decryptor v3.10 for gcc 2/3/4
In file included from ./inc/ncursesui.h:6:0,
from src/ncursesui.cpp:1:
functional: In static member function ‘static void _Function_handler<
    void({basic_string<char>} ...), _Bind<
        _Mem_fn<void (NcursesWindow::*)(basic_string<char>)>(
            NcursesWindow)> 
>::_M_invoke(const _Any_data &, {basic_string<char>} ...)’:
[STL Decryptor: Suppressed 1 more STL standard header message]
src/ncursesui.cpp:32:86:   instantiated from here
functional:1778:2: erreur: no match for call to ‘(
    _Bind<
        _Mem_fn<void (NcursesWindow::*)(basic_string<char>)>(
            NcursesWindow)>) (basic_string<char>)’

STL Decryptor reminders:
Use the /hdr:L option to see all suppressed standard lib headers
Use the /cand:L option to see all suppressed template candidates

最佳答案

string 参数的占位符是否在您的绑定(bind)调用中丢失?

试试这个:

bind(&NcursesWindow::add_string,&log_win,std::placeholders::_1);

成员函数有两个参数:隐藏的this 指针和一个std::string。您将第一个绑定(bind)到您的类的实例,另一个将保留,因此是占位符。

关于c++ - 在 C++ 中对带参数(字符串)的函数使用 std bind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9212601/

相关文章:

c++ - 为什么同时使用 #pragma once 和 include guard?

C++ double dispatch 观察者通知

c++ - 模板类成员?由于没有任何行为,如何适本地实现这种行为?

C++模板函数类型推导

javascript - JQuery 函数、包装和实时绑定(bind)

java - 线程中出现异常 "main"java.lang.UnsatisfiedLinkError : no JNTIest in java. library.path

c++ - 如何在一个构造函数中使用两个可变模板参数来绑定(bind)两个函数?

c - 为什么在查询根服务器时 ns_t_ns 比 ns_t_a 快?

python - Tkinter <Return> 功能不起作用

c++ - 复制构造函数是否重新初始化文件成员