gpt4 book ai didi

c++ - boost-asio 编译失败(C++ 类中的初始化列表)?

转载 作者:行者123 更新时间:2023-11-28 03:22:34 25 4
gpt4 key购买 nike

我正在尝试编译这段代码

#ifndef TCPIP_H
#define TCPIP_H

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

enum { max_length = 1024 };

class TCPIP
{
private:

boost::asio::io_service io_service;
tcp::resolver resolver;
tcp::resolver::query query;
tcp::resolver::iterator iterator;
tcp::socket s;

public:

TCPIP():resolver(io_service),
query("127.0.0.1", "2345"),
iterator(resolver.resolve(query)),
s(io_service),
boost::asio::connect(s, iterator)
{}
~TCPIP(){}
void IO()
{
std::cout << "Enter message: ";
char request[max_length];
std::cin.getline(request, max_length);
size_t request_length = std::strlen(request);
boost::asio::write(s, boost::asio::buffer(request, request_length));

char reply[max_length];
size_t reply_length = boost::asio::read(s,
boost::asio::buffer(reply, request_length));
std::cout << "Reply is: ";
std::cout.write(reply, reply_length);
std::cout << "\n";
}
};
#endif

我得到了这些错误

c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\boost\asio\detail\config.hpp|205|warning: #warning Please define _WIN32_WINNT or _WIN32_WINDOWS appropriately.|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\boost\asio\detail\config.hpp|206|warning: #warning For example, add -D_WIN32_WINNT=0x0501 to the compiler command line.|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\boost\asio\detail\config.hpp|207|warning: #warning Assuming _WIN32_WINNT=0x0501 (i.e. Windows XP target).|
C:\Documents and Settings\My Documents\project5\erlang_connect\TCPIP.h||In constructor 'TCPIP::TCPIP()':|
C:\Documents and Settings\My Documents\project5\erlang_connect\TCPIP.h|26|error: expected class-name before '(' token|
C:\Documents and Settings\My Documents\project5\erlang_connect\TCPIP.h|26|error: expected '{' before '(' token|
C:\Documents and Settings\My Documents\project5\erlang_connect\erlang_connect.cpp||In function 'int main()':|
C:\Documents and Settings\My Documents\project5\erlang_connect\erlang_connect.cpp|7|error: expected primary-expression before ';' token|
C:\Documents and Settings\My Documents\project5\erlang_connect\erlang_connect.cpp|5|warning: unused variable 'INTERFACE'|
||=== Build finished: 3 errors, 4 warnings ===|

基本上这个问题与初始化列表和声明 boost::asio::connect(s, iterator) 的问题有关。这是用于实现此目的的原始 Boost 源代码(boost 阻塞 tcp 客户端)。我尝试使用指针,但 boost 库不喜欢这个想法!

//
// blocking_tcp_echo_client.cpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

enum { max_length = 1024 };

int main(int argc, char* argv[])
{
try
{
if (argc != 3)
{
std::cerr << "Usage: blocking_tcp_echo_client <host> <port>\n";
return 1;
}

boost::asio::io_service io_service;

tcp::resolver resolver(io_service);
tcp::resolver::query query(tcp::v4(), argv[1], argv[2]);
tcp::resolver::iterator iterator = resolver.resolve(query);

tcp::socket s(io_service);
boost::asio::connect(s, iterator);

using namespace std; // For strlen.
std::cout << "Enter message: ";
char request[max_length];
std::cin.getline(request, max_length);
size_t request_length = strlen(request);
boost::asio::write(s, boost::asio::buffer(request, request_length));

char reply[max_length];
size_t reply_length = boost::asio::read(s,
boost::asio::buffer(reply, request_length));
std::cout << "Reply is: ";
std::cout.write(reply, reply_length);
std::cout << "\n";
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}

return 0;
}

最佳答案

在初始化列表中,表达式需要在父类的任一成员上调用构造函数。要解决此问题,只需将 boost::asio::connect() 移动到构造函数主体即可。

TCPIP()
: resolver(io_service), // calls resolver constructor
query("127.0.0.1", "2345"), // calls query constructor
iterator(resolver.resolve(query)), // calls iterator constructor
s(io_service) // calls socket constructor
{
boost::asio::connect(s, iterator);
}

此外,请考虑是否需要在 TCPIP 对象的整个生命周期内访问解析器、查询和迭代器。让它们成为 TCPIP 构造函数中的自动变量可能是值得的,结果如下:

TCPIP()
: s(io_service)
{
tcp::resolver resolver(io_service);
tcp::resolver::iterator iterator =
resolver.resolve(tcp::resolver::query(("127.0.0.1", "2345"));
boost::asio::connect(s, iterator);
}

关于c++ - boost-asio 编译失败(C++ 类中的初始化列表)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15042540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com