Lwin Htoo Ko
Lwin Htoo Ko

Reputation: 2406

'=' : left operand must be l-value

When I compile the following code, I got "error C2106: '=' : left operand must be l-value" at "m.msg_body[i].id = i;". When I comment out that line, there is not error. What is wrong with my code?

static const short MSG_DATA_MAX = 10;

struct MsgBodyData
{
    int id;
    string value;
};

class MsgBody
{
public:
    MsgBody()
    {
        len = 0;    
    }
    MsgBody(MsgBody & msg_obj);
    ~MsgBody() {}

    int length() { return len; }
    void setLength(int _len) { len = _len; }

    MsgBodyData operator[](short index)
    {
        if(index > -1 && index < MSG_DATA_MAX)
            return data[index];

        MsgBodyData dump_data;
        return dump_data;
    }

    void operator=(MsgBody & msg_obj)
    {
        len = msg_obj.length();
        for(int i = 0; i < len; ++i)
            data[i] = msg_obj[i];
    }

private:
    int len;
    MsgBodyData data[MSG_DATA_MAX];
};

MsgBody::MsgBody(MsgBody &msg_obj)
{
    operator=(msg_obj);
}

struct Msg
{
    int msg_id;
    string msg_title;
    MsgBody msg_body;
};


void sendMsg()
{
    Msg m;
    m.msg_id = 1;
    m.msg_title = "test_msg";

    int size = MSG_DATA_MAX;

    for(int i = 0; i < size; ++i)
    {
        m.msg_body[i].id = i;  // HERE I GOT ERROR !!!
        m.msg_body[i].value = "test_value";
    }
}

Upvotes: 2

Views: 13157

Answers (2)

Benjamin Lindley
Benjamin Lindley

Reputation: 103713

Your operator[] returns by value, which means that it makes a temporary copy and returns that. Temporaries are rvalues. You should modify your operator to return a reference. Actually, you should have two versions, a const version, which returns a const reference, and a non-const version, which returns a non-const reference.

Your method of handling an out of range index will have to change though. You can either throw an exception, or simply leave it as undefined behavior, just be sure to document it. Another option would be to have a dummy MsgBodyData object in the class that you return when you get a bad index, but that seems like a very silly design.

Upvotes: 4

Your operator[] returns a temporary (rvalue), which makes each one of its members an rvalue. The language forbids assigning to an rvalue (to be precise, assigning to an rvalue of non-class type), and that is what the error is telling you.

The reason for that restriction is that since the left hand side will be destroyed at the end of the full expression, the assignment would not make much sense (it will be forgotten with the temporary destruction).

If you meant to modify the element held inside the class, you need to return a reference (lvalue) to the stored element, rather than a copy.

Upvotes: 3

Related Questions