
Hi,
I have been trying very hard to "compile" this code. What is the reason
for the compilation errors in the "async_write" call.
#include

handler must be:
* @code void handler(
* const boost::system::error_code& error, // Result of operation.
*
* std::size_t bytes_transferred // Number of bytes written from the
* // buffers. If an error occurred,
* // this will be less than the sum
* // of the buffer sizes.
* ); @endcode
2009/11/18 Lloyd
Hi,
I have been trying very hard to "compile" this code. What is the reason for the compilation errors in the "async_write" call.
#include
#include #include using boost::asio::ip::tcp; class aa { public: void testme() { char a[20]="<test data>"; boost::asio::io_service io; tcp::socket s(io); boost::asio::async_write(s,boost::asio::buffer(a,sizeof(a)),
boost::bind(&aa::handler,shared_from_this(),boost::asio::placeholders::error)); }
void handler(const boost::system::error_code& error) { }
private:
};
int main() {
aa x; x.testme(); return 0; }
The erros I got are: ====================
1>------ Build started: Project: del, Configuration: Release Win32 ------ 1>Compiling... 1>del.cpp 1>.\del.cpp(19) : error C2780: 'void boost::asio::async_write(AsyncWriteStream &,boost::asio::basic_streambuf<Allocator> &,CompletionCondition,WriteHandler)' : expects 4 arguments - 3 provided 1> C:\Program Files\boost140\boost/asio/write.hpp(510) : see declaration of 'boost::asio::async_write' 1>.\del.cpp(19) : error C2784: 'void boost::asio::async_write(AsyncWriteStream &,boost::asio::basic_streambuf<Allocator> &,WriteHandler)' : could not deduce template argument for 'boost::asio::basic_streambuf<Allocator> &' from 'boost::asio::mutable_buffers_1' 1> C:\Program Files\boost140\boost/asio/write.hpp(453) : see declaration of 'boost::asio::async_write' 1>.\del.cpp(19) : error C2784: 'void boost::asio::async_write(AsyncWriteStream &,boost::asio::basic_streambuf<Allocator> &,WriteHandler)' : could not deduce template argument for 'boost::asio::basic_streambuf<Allocator> &' from 'boost::asio::mutable_buffers_1' 1> C:\Program Files\boost140\boost/asio/write.hpp(453) : see declaration of 'boost::asio::async_write' 1>.\del.cpp(19) : error C2780: 'void boost::asio::async_write(AsyncWriteStream &,const ConstBufferSequence &,CompletionCondition,WriteHandler)' : expects 4 arguments - 3 provided 1> C:\Program Files\boost140\boost/asio/write.hpp(411) : see declaration of 'boost::asio::async_write' 1>.\del.cpp(19) : error C3861: 'shared_from_this': identifier not found 1>Build log was saved at "file://c:\Documents and Settings\Administrator\Desktop\del\del\Release\BuildLog.htm" 1>del - 5 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
______________________________________ Scanned and protected by Email scanner _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Best regards, PangYongQiang

Lloyd wrote:
Hi,
I have been trying very hard to "compile" this code. What is the reason for the compilation errors in the "async_write" call.
</snip> You do not inherit from enable_shared_from_this http://www.boost.org/doc/libs/1_40_0/libs/smart_ptr/enable_shared_from_this.... Note that shared_from_this will not work unless object is created on heap and placed in a shared_ptr. Creating asynchronous operation handlers on stack does not make much sense.

On Wed, 18 Nov 2009 14:24:10 +0530, Igor R
Note that shared_from_this will not work unless object is created on heap and placed in a shared_ptr.
Creating asynchronous operation handlers on stack does not make much sense.
It does make sense, if the handler is a lightweight copiable object.
What if the handler take so much time in execution? Thanks, Lloyd ______________________________________ Scanned and protected by Email scanner

It does make sense, if the handler is a lightweight copiable object.
What if the handler take so much time in execution?
I don't understand how the execution time is relevant for this issue. Your handler must outlive your async. event, i.e. the object you pass to async_write must be alive when the write operation is complete. You can achive this in various ways, among them - by passing a smart pointer to a heap-allocated object, or by passing an object by-value.

Note that shared_from_this will not work unless object is created on heap and placed in a shared_ptr.
Thanks. Now it works. While binding I passed "this" instead of "shared_ptr", like this boost::bind(&aa::HandleWrite,this/*shared_from_this()*/,boost::asio::placeholders::error)
Creating asynchronous operation handlers on stack does not make much sense.
Why do you say this? Is there any chance of crashing the application? ______________________________________ Scanned and protected by Email scanner

Lloyd wrote:
Creating asynchronous operation handlers on stack does not make much sense.
Why do you say this? Is there any chance of crashing the application?
Correction of my previous statement: It does not make sense to pass member function bound to a stack allocated object as an asynchronous operation handler. Passed handler must remain valid until async operation is completed i.e. until it gets called. The shared_ptr trick is a way to ensure that, because handler itself owns a shared pointer to the object in question thus assuring its still valid. Your example is perfectly safe because it will do nothing - you are not running io_service in a separate thread, so async operation will never even start. You might want to have a look at asynchronous examples in asio documentation.
participants (4)
-
Igor R
-
ipangth
-
Juraj Ivančić
-
Lloyd