INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Jobs from Indeed

C++ STL Map

C++ STL Map

(OP)

I am getting a strange build error message when creating a simple STL map.

"stl_function.h passing `const CWord508' as `this' argument of `bool CWord508::operator<(CWord508)' discards qualifiers"

The error is triggered on the map.insert(<key object, other object> line

I have provided a operator < function on the key object class:

class CWord508 {
      char str[20];
public:
       CWord508();
       CWord508( char *s );
       char *get();
       
// must define operator "less than" for CWord508 objects
       bool operator<( CWord508 a);
       bool operator==( CWord508 a);
};

the c++ code is in separate file:
bool CWord508::operator<( CWord508 a)
{
     return (strcmp( str, a.get() ) < 0 );
}

I cannot see an obvious flaw with the code; any help / suggestions welcome.
NB I have tried providing operator> as well just in case.

Thanks

 

RE: C++ STL Map

Change definitions to

CODE

       bool operator<( const CWord508& a) const;
       bool operator==( const CWord508& a) const;

RE: C++ STL Map

(OP)
Thanks, that was a helpful reply,

I tried that adjustment and get the same error msg, reported, albeit in a different place:

'In member function bool CWord508::operator<(const CWord508&)'
passing const CWord508' as `this' argument of `char* CWord508::get()' discards qualifiers"

bool CWord508::operator<( const CWord508& a) const
{
   return ( str < a.get() );  // error reported here
}
char *CWord508::get()  const
{
   return str;
}

//
I remain baffled as to how to work round this build error

Thanks for any suggestions
 

RE: C++ STL Map

Hmm, it's a very very dangerous class...

CODE

class CWord508
{      
public:
  CWord508();
  CWord508(const char *s);
  char *get() { return str; } // why "get"?
  const char* get() const { return str; }
  // must define operator "less than" for CWord508 objects
  bool operator <(const CWord508& a) const {
       return strcmp(str,a.str) < 0; // (or a.get() ;)
  }
  bool operator==(const CWord508& a) const {
       return strcmp(str,a.str) == 0;
  }
private:
  char str[20];
};
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close