2

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;

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?

Any suggestions are welcome!

Update

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: no matching function for call to ‘boost::asio::basic_socket_acceptor::basic_socket_acceptor()’

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: 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]

Roy08
  • 253
  • 2
  • 7
  • 21
  • It would help if you posted the actual compiler errors – Tom Knapen Oct 21 '13 at 17:43
  • Thank your for your comment! i have added the compiler errors – Roy08 Oct 22 '13 at 08:00
  • Constructor definition syntax is as follows (note where the body {} block starts): CommunicationModuleTCPIP::CommunicationModuleTCPIP(bool Server, string IPAdress, int PortNumber) : Endpoint(.......), Acceptor(......), Sock(....) { /*body*/} – Igor R. Oct 22 '13 at 09:45
  • Thanks for your comment, made the syntax correct but still have the samee errors – Roy08 Oct 22 '13 at 09:57

1 Answers1

2

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.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Thank you for your answer i have added the header file code and the constructor code. – Roy08 Oct 22 '13 at 08:01
  • @Roy08: Now you've got the opening { in the wrong place. Move it after the initializer list, to mark the start of the constructor body. – Mike Seymour Oct 22 '13 at 21:52