当我编译以下代码时,我在“m.msg_body[i].id = i;”处得到“错误 C2106:'=':左操作数必须是左值”。当我注释掉该行时,没有错误。我的代码有什么问题?
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";
}
}