# Include <iostream>
# Include <boost/Bind. HPP>
# Include <boost/date_time/posix_time/posix_time.hpp>
# Include <boost/ASIO. HPP>
Void print (const boost: System: error_code & E,
Boost: ASIO: deadline_timer * t, int * count)
{
If (* count <5)
{
STD: cout <* count <"";
++ (* Count );
T-> expires_at (t-> expires_at () + boost: posix_time: seconds (1 ));
T-> async_wait (boost: BIND (print,
Boost: ASIO: placeholders: error, T, count ));
}
}
Int main ()
{
Boost: ASIO: io_service IO;
Int COUNT = 0;
Boost: ASIO: deadline_timer T (Io, boost: posix_time: seconds (1 ));
T. async_wait (boost: BIND (print,
Boost: ASIO: placeholders: error, & T, & COUNT ));
Io. Run ();
STD: cout <"final count is:" <count <STD: Endl;
Return 0;
}
Compile the above content (ASIO official asynchronous timer tutorial) in vs2005 with an error that contains windows. h header file error, although we did not write contains windows. h header file declaration, but the microsec_time_clock.hpp header file in the ASIO library contains windows. h header file. Therefore, the method for modifying this scheme includes windows. h. Write a macro definition before the header file:
# Ifdef boost_has_ftime
# Define win32_lean_and_mean // This line was added later
# Include <windows. h>
# Endif
At this point, recompile, there may be a failure to find the libboost_date_time-vc80-mt-s-1_35.lib library file, the solution is to copy the libboost_date_time-vc80-mt-1_35.lib library file, and then name it the libboost_date_time-vc80-mt-s-1_35.lib, as to why it is okay to do so, I am not very clear.
Recompile, you may also see the error of not finding the libboost_system-vc80-mt-s-1_35.lib library file, as above, rename the libboost_system-vc80-mt-1_35.lib library file to the libboost_system-vc80-mt-s-1_35.lib.
Note:
1. vc80 -- indicates vs2005
2. If you do not have the above libraries, Google will introduce how to compile the boost library. There are many Google libraries. Since boost is used, it is inevitable that some boost libraries need to be compiled. I have not learned how to compile only required libraries, so don't ask me!
3. I hope this record will provide a method to attract others who encounter similar problems.
Refer:
Question about the windows. h header file error:
Http://www.nabble.com/WinSock.h-has-already-been-included-td18329342.html
Postscript:
Later, another implementation method was provided, but many errors were found during compilation, so it was recorded here.
# Include <iostream>
# Include <boost/ASIO. HPP>
# Include <boost/Bind. HPP>
# Include <boost/date_time/posix_time/posix_time.hpp>
class printer
{< br> Public:
printer (boost: ASIO: io_service & Io)
:_ timer (Io, boost:: posix_time: seconds (1)
, _ count (0)
{< br> _ timer. async_wait (boost: bind (boost: mem_fn (& printer: print), this);
// _ timer. async_wait (boost: BIND (& printer: print, this); // compilation error. modify it to the preceding method.
}
~ Printer ()
{< br> STD: cout <"final count is" <_ count <"" }< br> void print ()
{< br> If (_ count <5)
{< br> STD :: cout <_ count _ count ++;
_ timer. expires_at (_ timer. expires_at () + boost: posix_time: seconds (1);
_ timer. async_wait (boost: bind (boost: mem_fn (& printer: print), this);
// _ timer. async_wait (boost: BIND (& printer: print, this); // compilation error. modify it to the preceding method.
}
}
Protected:
PRIVATE:
Boost: ASIO: deadline_timer _ timer;
Int _ count;
};
Int main ()
{
// Boost: ASIO: io_service IO;
Boost: ASIO: io_service IO;
Printer P (IO );
Io. Run ();
Return 0;
}