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

标签 c++ boost timer boost-asio

我试试这个: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/

相关文章:

java - 是否已经有一个适用于 android 的 StopWatch 类,为什么我的实现不起作用?

c++ - 了解继承多个空类时 gcc/clang 与 msvc2015 之间的不同填充规则

c++ - for循环后面的if语句是什么意思?

c++ - 继承运算符 +()

c++ - 使用 Prim 算法计算最小生成树 : how to make it simple?

java - Android 定时器/时钟

c++ - 应用程序无法启动,因为它无法找到或加载 QT 平台插件 "windows"

c++ - boost asio有状态套接字接口(interface)

c++ - 将时间字符串转换为持续时间

c - Linux 内核延迟,低于 jiffies,没有忙等待