Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / January 2007

Tip: Looking for answers? Try searching our database.

overloading operators for STL operations

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
PaulH - 12 Jan 2007 17:48 GMT
I have a std::vector< ICMP_ECHO_REPLY > container that I'd like to be
able to do such wonderful STL operations as max_element(),
accumulate(), greater_equal(), etc... So, I decided I would create a
class to contain that information and overload the necessary operators.

The idea is that I should be able to do something like this:

std::vector< ICMP_ECHO_REPLY > container;
ICMP_ECHO_REPLY reply;
container.push_back( reply );
//...
std::vector< ICMP_ECHO_REPLY >::const_iterator itMax = max_element(
container.begin() container.end() );

Thus far, I've got something like the class below, but I the error:
error C2678: binary '<' : no operator found which takes a left-hand
operand of type 'const CEchoReply' (or there is no acceptable
conversion)

So, first, I'd like to know if there is a better way of doing this.
Second, what is it I'm doing wrong here?

Thanks,
PaulH

class CEchoReply
{
public:
   ICMP_ECHO_REPLY _reply;
   CEchoReply()
   {
       ZeroMemory( &_reply, sizeof( ICMP_ECHO_REPLY ) );
   };
   CEchoReply( const ICMP_ECHO_REPLY& reply )
   {
       memcpy( &_reply, &reply, sizeof( ICMP_ECHO_REPLY ) );
   };
   ~CEchoReply()
   {
   };
   operator ICMP_ECHO_REPLY() const { return _reply; };
   bool operator < ( const ICMP_ECHO_REPLY& lhs )
   {
       return lhs.RoundTripTime < _reply.RoundTripTime;
   };
};
Ben Voigt - 12 Jan 2007 18:59 GMT
>I have a std::vector< ICMP_ECHO_REPLY > container that I'd like to be
> able to do such wonderful STL operations as max_element(),
[quoted text clipped - 38 lines]
>    operator ICMP_ECHO_REPLY() const { return _reply; };
>    bool operator < ( const ICMP_ECHO_REPLY& lhs )
bool operator < ( const ICMP_ECHO_REPLY& lhs ) const // <<- need the word
const there
>    {
>        return lhs.RoundTripTime < _reply.RoundTripTime;
>    };
> };
David Wilkinson - 12 Jan 2007 21:00 GMT
> I have a std::vector< ICMP_ECHO_REPLY > container that I'd like to be
> able to do such wonderful STL operations as max_element(),
> accumulate(), greater_equal(), etc... So, I decided I would create a
> class to contain that information and overload the necessary operators.

Paul:

Please don't multi-post.

You have a lot of answers in microsoft.public.vc.language.

David Wilkinson

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.