C++ – ‘=’ : left operand must be l-value

c

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";
    }
}

Best Answer

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.

Related Topic