gpt4 book ai didi

c++ - 来自其他类的 asio 计时器

转载 作者:太空宇宙 更新时间:2023-11-04 13:22:22 25 4
gpt4 key购买 nike

我试试这个:timer tuto .但是 void 打印在另一个类中。当我调用 main 中的函数时:

t.async_wait(&class::print);

我有这个错误:

error : must use '.*' or '->*' to call pointer-to-member function in 'boost::asio::detail::lvref<void (class::*)(const boost::system::error_code&)>() (...)', e.g. '(... ->* boost::asio::detail::lvref<void (class::*)(const boost::system::error_code&)>())

我不知道如何解决这个问题:s

You will have to bind the object instance, like: t.async_wait(boost::bind(&Class::print, this, _1)); Or if the instance is not this, use another (smart) pointer that points to a Class. You can use a reference, but that requires a reference wrapper to prevent bind from copying the bound argument: Class c; // ... t.async_wait(boost::bind(&Class::print, boost::ref(c), _1));

我已经试过了,但是我有一个错误:

error : 'void (class::*)(const boost::system::error_code&)' is not a class, struct, or union type typedef typename F::result_type type;

You need to handle the parameter that the print method expects. Something like this: t->async_wait(boost::bind(&Class::print, this, boost::asio::placeholders::error)); Take a look at this tutorial http://www.boost.org/doc/libs/1_59_0/doc/html/boost_asio/tutorial/tuttimer3.html

我试过了,但我有一个我不明白的错误

error : pointer to member type 'void (class::)(const boost::system::error_code&)' incompatible with object type 'windows'
BOOST_MEM_FN_RETURN (get_pointer(u)->*f_)(b1);
^

最佳答案

您必须绑定(bind)对象实例,例如:

t.async_wait(boost::bind(&Class::print, this, _1));

或者如果实例不是this,则使用另一个指向Class 的(智能)指针。您可以使用引用,但这需要引用包装器来防止绑定(bind)复制绑定(bind)参数:

Class c;

// ...
t.async_wait(boost::bind(&Class::print, boost::ref(c), _1));

关于c++ - 来自其他类的 asio 计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34710668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com