C++11 std::bind无法调用类成员函数:C++;
我必须执行C++11 std::bind无法调用类成员函数:C++;,c++11,boost,C++11,Boost,我必须执行阀门打开功能(在指定的时间内)。 我正在使用boost::asio::deadline\u计时器 我的班级成员打开阀门的功能是: bool Valves::valveOpen(ValveType type) { switch (type) { case eVentValve: tblMap_.digitalInput[eVentValveK1].setBit(); if (tblMa
阀门打开功能(在指定的时间内)
。
我正在使用boost::asio::deadline\u计时器
我的班级成员打开阀门的功能是:
bool Valves::valveOpen(ValveType type)
{
switch (type)
{
case eVentValve:
tblMap_.digitalInput[eVentValveK1].setBit();
if (tblMap_.digitalOutput[eOutK1VentValve].getBit())
{
isVentOpen_ = true;
}
return isVentOpen_;
case eVacuumPumpValve:
....
....
}
bool Valves::valveClose(ValveType type)
{
switch (type)
{
case eVentValve:
tblMap_.digitalInput[eVentValveK1].clearBit();
if (!tblMap_.digitalOutput[eOutK1VentValve].getBit())
{
isVentOpen_ = false;
}
return !isVentOpen_;
case eVacuumPumpValve:
....
....
}
关闭阀门的类成员功能为:
bool Valves::valveOpen(ValveType type)
{
switch (type)
{
case eVentValve:
tblMap_.digitalInput[eVentValveK1].setBit();
if (tblMap_.digitalOutput[eOutK1VentValve].getBit())
{
isVentOpen_ = true;
}
return isVentOpen_;
case eVacuumPumpValve:
....
....
}
bool Valves::valveClose(ValveType type)
{
switch (type)
{
case eVentValve:
tblMap_.digitalInput[eVentValveK1].clearBit();
if (!tblMap_.digitalOutput[eOutK1VentValve].getBit())
{
isVentOpen_ = false;
}
return !isVentOpen_;
case eVacuumPumpValve:
....
....
}
我正在尝试实现以下计时器操作
bool Valves::valveTimedOpen(ValveType type, int sec)
{
boost::asio::io_service io;
switch (type)
{
case eVentValve:
{
std::bind(&Valves::valveOpen, this, type); //Here
boost::asio::deadline_timer t(io, boost::posix_time::seconds(sec));
t.async_wait(std::bind(&Valves::valveClose, this, type));
boost::thread th(boost::bind(&boost::asio::io_service::run, &io));
return true;
}
case eVacuumPumpValve:
.....
.....
}
代码点击此处的行
,即
std::bind(&Valves::valveOpen,this,type)代码>但它不会转到bool Valves::valveOpen(ValveType)
函数
有人能告诉我这个代码的问题吗?变量io
和t
一旦退出valveTimedOpen
就超出范围。您需要重新考虑与boost asio组件交互的方式,例如,io_服务
可能是您的类的成员,并且计时器可以动态分配,需要在完成处理程序中删除
此外,请记住,如果您计划重新使用io\u服务
对象,您还需要在再次调用run
之前将其重置。变量io
和t
退出范围。您需要重新考虑与boost asio组件交互的方式,例如,io_服务
可能是您的类的成员,并且计时器可以动态分配,需要在完成处理程序中删除
auto fn = std::bind(&Test::Open, shared_from_this(), std::placeholders::_1);
fn(type);
此外,请记住,如果您计划重新使用io\u服务
对象,您还需要在再次调用run
之前将其重置。变量io
和t
退出范围。您需要重新考虑与boost asio组件交互的方式,例如,io_服务
可能是您的类的成员,并且计时器可以动态分配,需要在完成处理程序中删除
auto fn = std::bind(&Test::Open, shared_from_this(), std::placeholders::_1);
fn(type);
此外,请记住,如果您计划重新使用io\u服务
对象,您还需要在再次调用run
之前将其重置。变量io
和t
退出范围。您需要重新考虑与boost asio组件交互的方式,例如,io_服务
可能是您的类的成员,并且计时器可以动态分配,需要在完成处理程序中删除
auto fn = std::bind(&Test::Open, shared_from_this(), std::placeholders::_1);
fn(type);
另外,请记住,如果您计划重新使用io\u服务
对象,您还需要在再次调用run
之前重置它
auto fn = std::bind(&Test::Open, shared_from_this(), std::placeholders::_1);
fn(type);
正确调用Open()
io\u服务
和boost::deadline\u计时器
我必须按照@Ralf的建议成为班级成员
工作代码:
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>
#include <boost/asio.hpp>
class Test : public std::enable_shared_from_this <Test>
{
public:
Test() :io(), timer(io){}
void Open(int num);
void Close(int num);
void TimedOpen(int num, int dur);
void Run();
private:
boost::asio::io_service io;
boost::asio::deadline_timer timer;
};
void Test::Open(int num)
{
std::cout << "Open for Number : " << num << std::endl;
}
void Test::Close(int num)
{
std::cout << "Close for Number : " << num << std::endl;
}
void Test::TimedOpen(int num, int dur)
{
io.reset();
auto fn = std::bind(&Test::Open, shared_from_this(), std::placeholders::_1);
fn(num);
timer.expires_from_now( boost::posix_time::seconds(dur));
timer.async_wait(std::bind(&Test::Close, shared_from_this(), num));
Run();
std::cout << "Function Exiting" << std::endl;
}
void Test::Run()
{
boost::thread th(boost::bind(&boost::asio::io_service::run, &io));
}
int main()
{
auto t = std::make_shared<Test>();
t->TimedOpen(5, 5);
char line[128];
while (std::cin.getline(line, 128))
{
if (strcmp(line, "\n")) break;
}
return 0;
}
#包括
#包括
#包括
类测试:public std::从\u中启用\u共享\u
{
公众:
测试():io(),计时器(io){}
无效打开(整数);
无效关闭(int num);
void TimedOpen(int num,int dur);
无效运行();
私人:
boost::asio::io_服务io;
boost::asio::截止期计时器;
};
无效测试::打开(int num)
{
标准::cout
正确调用Open()
io\u服务
和boost::deadline\u计时器
我必须按照@Ralf的建议成为班级成员
工作代码:
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>
#include <boost/asio.hpp>
class Test : public std::enable_shared_from_this <Test>
{
public:
Test() :io(), timer(io){}
void Open(int num);
void Close(int num);
void TimedOpen(int num, int dur);
void Run();
private:
boost::asio::io_service io;
boost::asio::deadline_timer timer;
};
void Test::Open(int num)
{
std::cout << "Open for Number : " << num << std::endl;
}
void Test::Close(int num)
{
std::cout << "Close for Number : " << num << std::endl;
}
void Test::TimedOpen(int num, int dur)
{
io.reset();
auto fn = std::bind(&Test::Open, shared_from_this(), std::placeholders::_1);
fn(num);
timer.expires_from_now( boost::posix_time::seconds(dur));
timer.async_wait(std::bind(&Test::Close, shared_from_this(), num));
Run();
std::cout << "Function Exiting" << std::endl;
}
void Test::Run()
{
boost::thread th(boost::bind(&boost::asio::io_service::run, &io));
}
int main()
{
auto t = std::make_shared<Test>();
t->TimedOpen(5, 5);
char line[128];
while (std::cin.getline(line, 128))
{
if (strcmp(line, "\n")) break;
}
return 0;
}
#包括
#包括
#包括
类测试:public std::从\u中启用\u共享\u
{
公众:
测试():io(),计时器(io){}
无效打开(整数);
无效关闭(int num);
void TimedOpen(int num,int dur);
无效运行();
私人:
boost::asio::io_服务io;
boost::asio::截止期计时器;
};
无效测试::打开(int num)
{
标准::cout
正确调用Open()
io\u服务
和boost::deadline\u计时器
我必须按照@Ralf的建议成为班级成员
工作代码:
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>
#include <boost/asio.hpp>
class Test : public std::enable_shared_from_this <Test>
{
public:
Test() :io(), timer(io){}
void Open(int num);
void Close(int num);
void TimedOpen(int num, int dur);
void Run();
private:
boost::asio::io_service io;
boost::asio::deadline_timer timer;
};
void Test::Open(int num)
{
std::cout << "Open for Number : " << num << std::endl;
}
void Test::Close(int num)
{
std::cout << "Close for Number : " << num << std::endl;
}
void Test::TimedOpen(int num, int dur)
{
io.reset();
auto fn = std::bind(&Test::Open, shared_from_this(), std::placeholders::_1);
fn(num);
timer.expires_from_now( boost::posix_time::seconds(dur));
timer.async_wait(std::bind(&Test::Close, shared_from_this(), num));
Run();
std::cout << "Function Exiting" << std::endl;
}
void Test::Run()
{
boost::thread th(boost::bind(&boost::asio::io_service::run, &io));
}
int main()
{
auto t = std::make_shared<Test>();
t->TimedOpen(5, 5);
char line[128];
while (std::cin.getline(line, 128))
{
if (strcmp(line, "\n")) break;
}
return 0;
}
#包括
#包括
#包括
类测试:public std::从\u中启用\u共享\u
{
公众:
测试():io(),计时器(io){}
无效打开(整数);
无效关闭(int num);
void TimedOpen(int num,int dur);
无效运行();
私人:
boost::asio::io_服务io;
boost::asio::截止期计时器;
};
无效测试::打开(int num)
{
标准::cout
正确调用Open()
io\u服务
和boost::deadline\u计时器
我必须按照@Ralf的建议成为班级成员
工作代码:
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>
#include <boost/asio.hpp>
class Test : public std::enable_shared_from_this <Test>
{
public:
Test() :io(), timer(io){}
void Open(int num);
void Close(int num);
void TimedOpen(int num, int dur);
void Run();
private:
boost::asio::io_service io;
boost::asio::deadline_timer timer;
};
void Test::Open(int num)
{
std::cout << "Open for Number : " << num << std::endl;
}
void Test::Close(int num)
{
std::cout << "Close for Number : " << num << std::endl;
}
void Test::TimedOpen(int num, int dur)
{
io.reset();
auto fn = std::bind(&Test::Open, shared_from_this(), std::placeholders::_1);
fn(num);
timer.expires_from_now( boost::posix_time::seconds(dur));
timer.async_wait(std::bind(&Test::Close, shared_from_this(), num));
Run();
std::cout << "Function Exiting" << std::endl;
}
void Test::Run()
{
boost::thread th(boost::bind(&boost::asio::io_service::run, &io));
}
int main()
{
auto t = std::make_shared<Test>();
t->TimedOpen(5, 5);
char line[128];
while (std::cin.getline(line, 128))
{
if (strcmp(line, "\n")) break;
}
return 0;
}
#包括
#包括
#包括
类测试:public std::从\u中启用\u共享\u
{
公众:
测试():io(),计时器(io){}
无效打开(整数);
无效关闭(int num);
void TimedOpen(int num,int dur);
无效运行();
私人:
boost::asio::io_服务io;
boost::asio::截止期计时器;
};
无效测试::打开(int num)
{
标准::cout