i具有解析标记参数的主函数。其中之一是IP地址字符串(V4(。我将值保存在char数组中。
i然后将char数组移至const字符串。
const std::string &str(ip_address);
进行了一些验证检查后,我想设置此IP地址以由我的代码使用。
boost::asio::ip::address a = boost::asio::ip::address::from_string(str);
但是,此行导致错误(Intellisense(:
在lambda中不能引用封闭功能的本地变量 身体在捕获列表中,除非它
和一个编译错误:
错误c3493:'str'不能被隐式捕获,因为没有 已指定默认捕获模式
如何在不硬编码的情况下设置IP地址?
注意:我有2个网络适配器,我需要每个程序都可以在每个程序上运行,并且我正在传递每个程序。
编辑:(完整代码部分(
CLASS::Main(int argc, char **argv)
: io_service(nullptr), server(nullptr), iso_parameters(nullptr), wcs_connection(nullptr), class_protocol(nullptr)
{
char c;
char ip_address[20];
//set_warehouse_argument(argc, argv);
while( -1 != (c = getopt(argc,argv,"w:i:a:")) )
{
switch(c)
{
case 'i':
mydat.instance = atoi(optarg);
break;
case 'w':
/* Processed by set_warehouse_argument */
break;
case 'a':
strcpy_s(ip_address, optarg);
break;
default:
usage(argv[0],"Invalid argument flag passed.");
exit(1);
}
}
const std::string &str(ip_address);
if(mydat.instance <= 0 || mydat.instance > 5)
{
usage(argv[0],"Invalid or missing instance passed (valid is 1-5).");
exit(1);
}
accept_handler = [this](boost::shared_ptr<iso::connection>& socket, const boost::system::error_code& error)
{
if(error != 0)
{
LOG_ERROR(mydat.instance) << "Error on accept: " << error.message();
}
else
{
/* Kick out the old client if present */
if(wcs_connection && wcs_connection->state() != tcp_connection::DISCONNECTED)
{
LOG_EVENT << "New connection. Closing old client.";
wcs_connection->close();
}
/* Initialize the connection and log the IP address */
pending_connection = socket;
auto now = std::chrono::steady_clock::now();
this->connection_timeout = now;
boost::asio::ip::tcp::socket &s = pending_connection->socket();
boost::system::error_code ec;
boost::asio::ip::address a = boost::asio::ip::address::from_string(str);
if(ec == 0)
{
LOG_EVENT << "Client (socket " << s.native_handle() << ") from " << a.to_string() << " connected.";
}
else
{
LOG_ERROR(mydat.instance) << "Error on looking up remote endpoint for new client: " << ec.message();
pending_connection->close();
pending_connection = nullptr;
}
}
};
}
我遇到了与lambda相关的错误,但我没有编码lambda。
您肯定编码了lambda
-accept_handler
,但您尚未捕获str
。将其更改为...
accept_handler = [this, str](boost::shared_ptr<iso::connection>& socket,
const boost::system::error_code& error)
话虽如此,您实际上并没有显示accept_handler
的声明,所以我仍然猜测这是问题的根源。