c++ 中 std bind 的用法对于带有参数(字符串)的函数

发布于 2025-01-04 04:55:05 字数 1306 浏览 1 评论 0原文

我正在尝试在我的记录器类中创建 std::functions 向量。当我尝试将一个方法绑定到我的 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

I'm trying to create a vector of std::functions in my logger class. When I try to bind a method to my std::function like that :

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

the add_string function being defined like :

void add_string(string text);

However, gcc (with the gfilt addon to try to understand the template errors) returns :

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琉璃繁缕 2025-01-11 04:55:05

您的绑定调用中是否缺少 string 参数的占位符?

试试这个:

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

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

Isn't there a placeholder for the string parameter missing in your bind call?

Try this:

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

The member function has two parameters: the hidden this pointer and a std::string. You bind the first to the instance of your class, and the other will stay, therefore the placeholder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文