0

My compiler is complaining that "Expression is not assignable" for the line of code handling the str.size function. I can't tell what I have wrong. Can someone help ? I'm passing a string into a function and trying to get the length of it.

   int ValueString::value(string str)const
   {
        int length;
        str.size() = length;
        return length;

    }

2 Answers 2

4

str.size() returns an R-value, which cannot be assigned to.

length is an L-value and can be assigned to.

You mean:

int ValueString::value(const string& str)const
{
    int length;
    length = str.size();
    return length;
}

This could, of course, be simplified to:

int ValueString::value(const string& str) const
{
    return str.size();
}

It could be simplified even further than that, perhaps...

Sign up to request clarification or add additional context in comments.

Comments

3

The compiler message says it all. You can't assign to the result of the function call size(). I think you probably mean int length = str.size().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.