Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ STL Map

Status
Not open for further replies.

jefe9

Programmer
Mar 3, 2002
7
GB

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::eek:perator<(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::eek:perator<( 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

 
Change definitions to
Code:
       bool operator<( const CWord508& a) const;
       bool operator==( const CWord508& a) const;
 
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::eek:perator<(const CWord508&)'
passing const CWord508' as `this' argument of `char* CWord508::get()' discards qualifiers"

bool CWord508::eek:perator<( 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
 
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];
};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top