使用 boost::bind ...我做错了什么
Usage of boost::bind ... what am I doing wrong
我改编了以下问题的第一个答案中的代码来制作周期性计时器:
How do I make the boost/asio library repeat a timer?
我删除了 "count" 变量,因为我想重复执行的方法不采用任何参数进行计算。
编译失败并出现以下错误:
Error C2825 'F': must be a class or namespace when followed by '::' C:\.conan\wkypad\include\boost\bind\bind.hpp 75
Error C2510 'F': left of '::' must be a class/struct/union C:\.conan\wkypad\include\boost\bind\bind.hpp 75
Error C3646 'type': unknown override specifier C:\.conan\wkypad\include\boost\bind\bind.hpp 75
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int C:\.conan\wkypad\include\boost\bind\bind.hpp 75
Error C2039 'type': is not a member of 'boost::_bi::result_traits<R,F>' C:\.conan\wkypad\include\boost\bind\bind.hpp 1284
我做错了什么?
我想我对 boost::bind 的用法是错误的,但我无法找出确切原因。
这是我的代码的相关部分
class ProjectBrowserComponent ...
private:
void fetchData(const boost::system::error_code& /*e*/,
boost::asio::deadline_timer* t);
std::vector<Project> projects;
ProjectsController pc;
std::future<std::vector<Project>> projectsFuture;
int interval_secs = 1;
...
template<typename R>
bool isReady(std::future<R> const& f)
{
Logger::writeToLog("check future");
return f.wait_for(std::chrono::seconds(-1)) == std::future_status::ready;
}
void ProjectBrowserComponent::initData() {
Logger::writeToLog("requesting projects");
projectsFuture = std::async(std::launch::async, &ProjectsController::getProjects, &pc);
Logger::writeToLog("requested projects");
boost::asio::io_service io_service;
boost::asio::deadline_timer timer(io_service, boost::posix_time::seconds(interval_secs));
timer.async_wait(boost::bind(&ProjectBrowserComponent::fetchData, boost::asio::placeholders::error, &timer));
io_service.run();
}
void ProjectBrowserComponent::fetchData(const boost::system::error_code& /*e*/,
boost::asio::deadline_timer* timer) {
if (isReady(projectsFuture)) {
projects = projectsFuture.get();
std::cout << "got projs";
}
else {
timer->expires_at(timer->expires_at() + boost::posix_time::seconds(interval_secs));
// Posts the event
timer->async_wait(boost::bind(&ProjectBrowserComponent::fetchData, boost::asio::placeholders::error, timer));
}
}
这个怎么样:
timer.async_wait(boost::bind(&ProjectBrowserComponent::fetchData, this, boost::asio::placeholders::error, &timer));
如果绑定成员函数,则还必须将它们绑定到 class 对象的正确实例。在这里,我将其绑定到 this
,从代码的外观来看,我认为这是您正在做的事情。
sehe 来自评论的注释:
请务必注意,这必须在整个异步操作期间保持有效。实现此目的的一种流行方法是使 ProjectBrowserComponent
继承自 enable_shared_from_this
并绑定到 shared_from_this()
我改编了以下问题的第一个答案中的代码来制作周期性计时器: How do I make the boost/asio library repeat a timer?
我删除了 "count" 变量,因为我想重复执行的方法不采用任何参数进行计算。
编译失败并出现以下错误:
Error C2825 'F': must be a class or namespace when followed by '::' C:\.conan\wkypad\include\boost\bind\bind.hpp 75
Error C2510 'F': left of '::' must be a class/struct/union C:\.conan\wkypad\include\boost\bind\bind.hpp 75
Error C3646 'type': unknown override specifier C:\.conan\wkypad\include\boost\bind\bind.hpp 75
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int C:\.conan\wkypad\include\boost\bind\bind.hpp 75
Error C2039 'type': is not a member of 'boost::_bi::result_traits<R,F>' C:\.conan\wkypad\include\boost\bind\bind.hpp 1284
我做错了什么? 我想我对 boost::bind 的用法是错误的,但我无法找出确切原因。
这是我的代码的相关部分
class ProjectBrowserComponent ...
private:
void fetchData(const boost::system::error_code& /*e*/,
boost::asio::deadline_timer* t);
std::vector<Project> projects;
ProjectsController pc;
std::future<std::vector<Project>> projectsFuture;
int interval_secs = 1;
...
template<typename R>
bool isReady(std::future<R> const& f)
{
Logger::writeToLog("check future");
return f.wait_for(std::chrono::seconds(-1)) == std::future_status::ready;
}
void ProjectBrowserComponent::initData() {
Logger::writeToLog("requesting projects");
projectsFuture = std::async(std::launch::async, &ProjectsController::getProjects, &pc);
Logger::writeToLog("requested projects");
boost::asio::io_service io_service;
boost::asio::deadline_timer timer(io_service, boost::posix_time::seconds(interval_secs));
timer.async_wait(boost::bind(&ProjectBrowserComponent::fetchData, boost::asio::placeholders::error, &timer));
io_service.run();
}
void ProjectBrowserComponent::fetchData(const boost::system::error_code& /*e*/,
boost::asio::deadline_timer* timer) {
if (isReady(projectsFuture)) {
projects = projectsFuture.get();
std::cout << "got projs";
}
else {
timer->expires_at(timer->expires_at() + boost::posix_time::seconds(interval_secs));
// Posts the event
timer->async_wait(boost::bind(&ProjectBrowserComponent::fetchData, boost::asio::placeholders::error, timer));
}
}
这个怎么样:
timer.async_wait(boost::bind(&ProjectBrowserComponent::fetchData, this, boost::asio::placeholders::error, &timer));
如果绑定成员函数,则还必须将它们绑定到 class 对象的正确实例。在这里,我将其绑定到 this
,从代码的外观来看,我认为这是您正在做的事情。
sehe 来自评论的注释:
请务必注意,这必须在整个异步操作期间保持有效。实现此目的的一种流行方法是使 ProjectBrowserComponent
继承自 enable_shared_from_this
并绑定到 shared_from_this()