【发布时间】:2013-11-04 19:34:21
【问题描述】:
我正在尝试编写一个名为“Timer”的类,它围绕 boost::asio::deadline_timer 进行包装,并具有使每个 deadline_timer 在其自己的线程中运行(彼此独立)的附加功能。
不幸的是,我无法使用 boost::bind 调用我在自己的类中定义的 typedef(以下内容将使我的问题更清楚)。
在我的“Timer”类中,我的 asyncWait() 的签名如下(并调用了 deadline_timer.async_wait()):
template <typename WaitHandler>
void Timer::asyncWait(WaitHandler handler) { ... }
我正在尝试从“Server”类的方法中调用此方法,例如:
boost::scoped_ptr<Timer> mButton;
// some code
mButton->asyncWait(boost::bind(&Server::foo, this, boost::asio::placeholders::error));
使用方法:
void Server::foo(const boost::system::error_code& error) { ... }
但现在在链接器期间出现以下错误,我不明白:
error: undefined reference to 'void Timer::asyncWait<boost::_bi::bind_t<void, boost::_mfi::mf1<void, Server, boost::system::error_code const&>, boost::_bi::list2<boost::_bi::value<Server*>, boost::arg<1> (*)()> > >(boost::_bi::bind_t<void, boost::_mfi::mf1<void, Server, boost::system::error_code const&>, boost::_bi::list2<boost::_bi::value<Server*>, boost::arg<1> (*)()> >)'
collect2: ld returned 1 exit status
在 Server 方法中调用 mButton->asyncWait() 会打印出来。我不明白为什么它正在编译,但它无法链接。 “Timer”类作为共享库添加到编译中,因此它似乎不是这里的实际问题。请问有什么问题,我该如何解决?
【问题讨论】:
-
代码使用
boost::bind时,为什么标题要讲std::bind?这个问题有什么遗漏吗?这是一个错误吗? -
创建一个演示问题的最小示例。请务必包含您正在使用的编译/链接步骤。
-
对不起,应该是 boost::bind David,我会编辑和修复这个问题。感谢您指出。