C ++:使用模板和boost :: bind的链接器问题

时间:2013-11-04 19:34:22

标签: c++ boost linker

我正在尝试写一个名为" Timer"包含boost :: asio :: deadline_timer的附加功能,使每个deadline_timer在自己的线程中运行(彼此独立)。

不幸的是,我无法使用boost :: bind调用我在自己的类中定义的typedef(以下内容会使我的问题更清楚)。

在我的"计时器" class,我的asyncWait()的签名如下(并调用deadline_timer.async_wait()):

template <typename WaitHandler>
void Timer::asyncWait(WaitHandler handler) { ... }

我试图在类&#34; Server&#34;的方法中调用此方法,如下:

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-&gt; asyncWait()而打印出来的。我不知道为什么它正在编译但它无法链接。课程&#34;定时器&#34;作为共享库添加到编译中,所以它似乎不是这里的实际问题。请问有什么问题,我该如何解决?

1 个答案:

答案 0 :(得分:1)

最有可能的是,asynchWait()的实现在您实例化函数模板的时候是不可见的。确保在使用该函数时编译器可以看到该实现,例如通过在声明函数的同一个头中实现该函数。

说明:通常,编译器仅为非模板函数生成机器代码。对于模板函数,编译器将机器代码的生成推迟到这一点,其中模板实例化的实际类型是已知的。这是因为它可以在类型与类型之间产生巨大差异。取两个整数之和的代码很可能与两个容器或向量相加的代码非常不同。

相关问题