boost::asio socket as class member

Hi! I want to put a boost::asio::ip::tcp::socket as a private member of my class and then create the object in one of the class methods. I actually use this code: class myClass{ private: boost::asio::io_service io_service; boost::asio::ip::tcp::socket s; }; But this code don't work (don't compile). Why? -- View this message in context: http://boost.2283326.n4.nabble.com/boost-asio-socket-as-class-member-tp30477... Sent from the Boost - Users mailing list archive at Nabble.com.

You need to construct s with your io_service... you need to do that in
myClass().
Brian
On Wed, Nov 17, 2010 at 2:37 PM, Claude
Hi!
I want to put a boost::asio::ip::tcp::socket as a private member of my class and then create the object in one of the class methods.
I actually use this code:
class myClass{
private: boost::asio::io_service io_service; boost::asio::ip::tcp::socket s; };
But this code don't work (don't compile). Why? -- View this message in context: http://boost.2283326.n4.nabble.com/boost-asio-socket-as-class-member-tp30477... Sent from the Boost - Users mailing list archive at Nabble.com. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Claude wrote:
Hi!
I want to put a boost::asio::ip::tcp::socket as a private member of my class and then create the object in one of the class methods.
I actually use this code:
class myClass{
private: boost::asio::io_service io_service; boost::asio::ip::tcp::socket s; };
But this code don't work (don't compile). Why?
Wow, one I can answer.. Anyways, I suspect the compile error you're getting is "No default constructor available". The problem is just that, Socket doesn't have an "empty" constructor. The Socket constructor needs an boost::asio::io_service object to work. You can fix your code by adding a constructor that does the initialization, like so: class myClass{ public: myClass() : s(io_service) {} private: boost::asio::io_service io_service; boost::asio::ip::tcp::socket s; }; Hope that helps!

Ok, it now work! Thanks!! :-) -- View this message in context: http://boost.2283326.n4.nabble.com/boost-asio-socket-as-class-member-tp30477... Sent from the Boost - Users mailing list archive at Nabble.com.
participants (3)
-
Brian Budge
-
Claude
-
The Novice Coder