在类头文件中声明boost asio套接字,接受器和终结点 [英] Declaring a boost asio socket, acceptor and endpoint in a class headerfile
问题描述
我有一个用boost asio制成的TCP/IP服务器,它包装在一个类中.现在,我想在类头文件中声明套接字,eindpoint和acceptor,以便我可以使用该套接字的成员函数.服务器现在在类构造函数中运行.我尝试使用如下初始化列表:
I have a TCP/IP server made with boost asio that is wrapped in a class. Now i want declare the socket, eindpoint and acceptor in the class headerfile so that i can make memberfunctions that use the socket. The server runs now in the class constructor. I tried to use a initialiser list like below:
CommunicationModuleTCPIP::CommunicationModuleTCPIP(bool Server,
string IPAdress,
int PortNumber)
: Sock(new boost::asio::ip::tcp::socket(IOService));
{
// Constructor code
但这会导致编译器错误.像这样在标头中声明袜子:
But this gives compiler errors. Sock is declared in the header like this:
SmartSocket Sock;
SmartSocket是通过以下方式定义的共享指针.
And SmartSocket is a shared pointer that is defined the following way.
typedef boost::shared_ptr<boost::asio::ip::tcp::socket> SmartSocket;
有人可以告诉我是否可以在头文件中声明套接字,接受器和终结点并初始化这些对象吗?
Can anybody tell me is it is possible to declare a socket, acceptor and endpoint in the headerfile and intialize these objects?
欢迎提出任何建议!
下面是te类头文件代码和构造函数代码:
Below is te class headerfile code and the constructor code:
带有端点,套接字和接受者声明的头文件:
Header file with Endpoint, socket and acceptor declarations:
public:
// Constructor and destructor
CommunicationModuleTCPIP(bool Server,
string IPAdress,
int PortNumber);
~CommunicationModuleTCPIP();
int Connect();
int Disconnect();
int SendData(SmartSocket sock);
int RecieveData(SmartSocket sock);
int Send(SmartSocket Sock);
int Recieve(SmartSocket sock);
int CheckConnection();
char SendBuffer[20];
char RecvBuffer[20];
SmartSocket Sock;
tcp::endpoint Endpoint;
boost::asio::ip::tcp::acceptor Acceptor;
构造函数,当我在构造函数中创建它们时,注释中的是套接字,端点和接受器.这可行,但我想使这些对象成为类的一部分,以便可以在代码的其他部分的成员函数中使用它们(如果可能的话).
Constructor, in comments are the socket, endpoint and acceptor when i create them in the constructor. This works but i want to make these objects part of the class so that i can use them in member functions in other part of the code (if this is possible?).
CommunicationModuleTCPIP::CommunicationModuleTCPIP(bool Server,
string IPAdress,
int PortNumber){
:Endpoint(ip::address::from_string(IPAdress),PortNumber),
Acceptor(IOService, Endpoint),
Sock(new boost::asio::ip::tcp::socket(IOService)
if(Server){
cout << "Setting up server" << endl;
//ip::tcp::endpoint Endpoint(ip::address::from_string(IPAdress),PortNumber);
// Create acceptor
//boost::asio::ip::tcp::acceptor Acceptor(IOService, Endpoint);
// Create socket
//SmartSocket Sock(new boost::asio::ip::tcp::socket(IOService));
cout << "Before accept..." << endl;
// Waiting for client
Acceptor.accept(*Sock);
cout << "Server set up" << endl;
}
我得到的编译器错误如下:
The compiller errors that i get are as followed:
\ CommunicationModuleTCPIP.cpp:6:30:fout:没有匹配的函数可用于调用"boost :: asio :: basic_socket_acceptor :: basic_socket_acceptor()"
\CommunicationModuleTCPIP.cpp:6:30: fout: no matching function for call to ‘boost::asio::basic_socket_acceptor::basic_socket_acceptor()’
fout::"令牌之前的预期主要表达式 .. \ CommunicationModuleTCPIP.cpp:7:2:fout:预期的';'在':'令牌之前 .. \ CommunicationModuleTCPIP.cpp:199:2:fout:在输入末尾应为}"
fout: expected primary-expression before ‘:’ token ..\CommunicationModuleTCPIP.cpp:7:2: fout: expected ‘;’ before ‘:’ token ..\CommunicationModuleTCPIP.cpp:199:2: fout: expected ‘}’ at end of input
C:\ boost_1_54_0/boost/asio/error.hpp:244:45:定义op:"boost :: asio :: error :: system_category",但未使用[-Wunused-variable] C:\ boost_1_54_0/boost/asio/error.hpp:246:45:定义op:"boost :: asio :: error :: netdb_category",但未使用[-Wunused-variable] C:\ boost_1_54_0/boost/asio/error.hpp:248:45:让op:定义为"boost :: asio :: error :: addrinfo_category",但未使用[-Wunused-variable] C:\ boost_1_54_0/boost/asio/error.hpp:250:45:定义op:"boost :: asio :: error :: misc_category",但未使用[-Wunused-variable]
C:\boost_1_54_0/boost/asio/error.hpp:244:45: let op: ‘boost::asio::error::system_category’ defined but not used [-Wunused-variable] C:\boost_1_54_0/boost/asio/error.hpp:246:45: let op: ‘boost::asio::error::netdb_category’ defined but not used [-Wunused-variable] C:\boost_1_54_0/boost/asio/error.hpp:248:45: let op: ‘boost::asio::error::addrinfo_category’ defined but not used [-Wunused-variable] C:\boost_1_54_0/boost/asio/error.hpp:250:45: let op: ‘boost::asio::error::misc_category’ defined but not used [-Wunused-variable]
推荐答案
唯一明显的错误是初始化列表末尾的流氓;
.
The only obvious error is the rogue ;
at the end of the initialiser list.
如果删除时仍然有错误,也许您可以张贴一个完整的小代码示例来演示错误以及错误本身.否则,很难猜测其余代码可能在做什么.
If you still have errors when you remove that, perhaps you could post a small, complete code example that demonstrates the error, and the errors themselves. Otherwise, it's rather difficult to guess what the rest of your code might be doing.
这篇关于在类头文件中声明boost asio套接字,接受器和终结点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!