c++ - Error: Initial value of reference to non-const must be an lvalue -
getting error string class. intellisense not let me use strcmp compare object self-invoking object (i.e. *this).
i tried making own operator conversion function this, still giving me error.
what have change in code make work??
//overloaded comparison operators bool &string::operator<(const string & obj) { return strcmp(*this, obj) < 0 ? true : false; } //operator conversion function string::operator char const * () const { return mstr; }
you returning reference local variable. return value. can simplify return expression, , make method const
, since comparing 2 objects should not change either of them:
bool string::operator<(const string & obj) const { return strcmp(*this, obj) < 0; }
although not sure strcmp
can deal 2 string
s, passing it. judgning previous question, need
return strcmp(mstr, obj.mstr) < 0;
Comments
Post a Comment