expression evaluation fails to succeed

josh BAUER 166 Reputation points
2020-11-29T20:10:27.7+00:00
class myString {
public:
    myString(int number) {}
    myString(myString& paramMyString) {}
    myString(myString&& paramMyString) {}
    friend myString operator+(const char stString, const myString& ndString);
    friend myString operator+(const myString& stString, const char* ndString);
    friend myString operator+(const myString& stString, const myString& ndString);

DWORD length() {
return 9;
}
myString& operator=(myString& rhs)
{
return *this;
}
myString& operator=(myString&& rhs)
{
return this;
}
};
myString operator+(const char
stString, const myString& ndString)
{
myString rr(0);
return rr;
}
myString operator+(const myString& stString, const char* ndString)
{
myString rr(0);
return rr;
}
myString operator+(const myString& stString,const myString& ndString)
{
int ndString_len=ndString.length();//<----HERE ERROR
myString rr(0);
return rr;
}
Above is code for class that I defined + operator. Following fails to evaluate.

/*
myString kk(8);
myString pp = (kk+" ")+"h";

I entered brackets so It should better understand in which priority compiler should execute
overloading operands but it fails to do operator + on (kk+" ") first and secondly on result + "h".
Can I do it?

How to do it?*/

I added const before each myString& and I am getting other kind of errors:
IN myString operator+(const myString& stString,const myString& ndString)
I AM GETTING error in int ndString_len=ndString.length();

It says it is error about CONST word BUT I cannot understand how to make it working.
HOW CAN I FIX IT?
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,526 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 112.1K Reputation points
    2020-11-29T20:17:23.8+00:00

    Try this fix: *‘friend myString operator+( * const myString& stString, const char * ndString )’ and add const to all appropriate declarations and definitions.