Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

jefe9 (Programmer)
23 May 10 21:24

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

 
xwb (Programmer)
23 May 10 23:34
Change definitions to

CODE

       bool operator<( const CWord508& a) const;
       bool operator==( const CWord508& a) const;
jefe9 (Programmer)
26 May 10 8:07
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
 
ArkM (IS/IT--Management)
30 May 10 17:51
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];
};
 

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!

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