错误:表达式必须是可修改的左值 [英] error : expression must be a modifiable lvalue
本文介绍了错误:表达式必须是可修改的左值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到错误:
表达式必须是行中可修改的左值,
obj.name = ptr->名称
我试图将 obj 设为数组类型对象类似
for(int j=0 ;j<1;j++)
{
obj[j].id = ptr->id;
obj[j].balance= ptr->balance;
obj[j].name = ptr->name; //still getting error here.
obj[j].nic = ptr->nic;
}
return(obj);
}
但这也不起作用。
如果我注释掉错误并仅传递三个剩余值,它应该可以工作,但我在第一个输出后会收到垃圾值。
if i comment the error out and pass just three remaining values it should work but i receive garbage values after the first out put.
这是原始代码:
#include<iostream>
using namespace std;
struct bank
{
int id, nic;
float balance;
char name[20];
};
bank search(bank* );
void main()
{
bank data[2],mobj;
for(int i=0;i<2;i++)
{
cout<<"enter name: ";
cin>>data[i].name;
cout<<"enter id: ";
cin>>data[i].id;
cout<<"enter balance : ";
cin>>data[i].balance;
cout<<"enter nic : ";
cin>>data[i].nic;
}
mobj=search(data);
cout <<"balance of customer no. "<<mobj.balance<<endl;
cout<<"id is" <<mobj.id<<endl;
cout<< "nic is"<<mobj.nic<<endl;
system("pause");
}
bank search(bank *ptr)
{
int id;
cout<<"enter value you want to serch"<<endl;
cin>>id;
bank obj;
for(int i=0 ; i<2 ;i++)
{
if(ptr->id == id)
{
break;
}
ptr++;
}
obj.id = ptr->id;
obj.balance= ptr->balance;
obj.name = ptr->name; //error in this line(obj must be modifiable value)
obj.nic = ptr->nic;
return(obj);
}
请在您认为合适的情况下提供帮助!
please help as you see fit!
推荐答案
obj.name
是 char
的数组。您不能对数组进行分配。因此,如果您要坚持使用数组:
obj.name
is an array of char
. You cannot do assignments on arrays. So if you want to stick to arrays:
- 请参见多个值的c ++数组分配
- 使用
strcpy(obj.name,ptr->名称);
- see c++ array assignment of multiple values
- use
strcpy(obj.name, ptr->name);
,但我建议将其转换为 std ::字符串
...比使用数组要容易得多,在我看来,您计划使用 obj.name
作为字符串。这样就有了正确的字符串。
but I'd recommend to convert to std::string
... it's much easier to work with than arrays and it seems to me you plan to use obj.name
as string. So got with the proper strings.
这篇关于错误:表达式必须是可修改的左值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文