LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-11-2015, 06:08 AM   #1
rozii
LQ Newbie
 
Registered: May 2015
Posts: 1

Rep: Reputation: Disabled
lvalue required as left operand of assignment


//I get error when I write this code
#include<iostream>
using namespace std;
float v[]={10,15,20,25,30,35,40};
float me(int i);
int main()
{
cout<<"Values before change\n";
for(int i=0;i<5;i++)
{
cout<<"v["<<i<<"]="<<v[i]<<endl;
}
me(3)=99;
me(4)=100;
cout<<"\nValue after change"<<endl;
for(int i=0;i<5;i++)
{
cout<<"v["<<i<<"]="<<v[i]<<endl;
}
return 0;
}
float me(int l)
{
return v[l];
}
//And when I use return by reference the error is gone
#include<iostream>
using namespace std;
float v[]={10,15,20,25,30,35,40};
float& me(int i);
int main()
{
cout<<"Values before change\n";
for(int i=0;i<5;i++)
{
cout<<"v["<<i<<"]="<<v[i]<<endl;
}
me(3)=99;
me(4)=100;
cout<<"\nValue after change"<<endl;
for(int i=0;i<5;i++)
{
cout<<"v["<<i<<"]="<<v[i]<<endl;
}
return 0;
}
float& me(int l)
{
return v[l];
}
//But I cannot Understand Why
 
Old 05-11-2015, 06:28 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,126
Blog Entries: 10

Rep: Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825
Welcome to LQ.

NOTE: Use [code][/code] tags around your code so that formatting occurs properly and it is readable. There is a link in my signature discussing this.

Recommend that this thread be moved to the PROGRAMMING forum for better exposure.

me() is a function. The first complaints are that you are doing this:
Code:
me(3)=99;
me(4)=100;
Is incomprehensible code to the compiler. The function me() returns a floating point value. Therefore you would assign a float variable as the RETURN from me(), example:
Code:
float result;

result = me(3);
Your second attempt may clear up the error, however it is nonsense code, you are not going to get any value of a result by assigning the output of a function to a scalar value. See my example for the correct thing to do.

Last edited by rtmistler; 05-11-2015 at 06:29 AM.
 
Old 05-11-2015, 06:36 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,214

Rep: Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696
can you please use [code]here comes your code[/code] to keep formatting. Also please post the full command you executed and the full error message.

Code:
me(3)=99;
# will first try yo call the function me with argument 3. that function will return
# in case of
float& me(int i)
# a reference to the 3rd element of the array, that can be used as lvalue
# that means me(3) can be set (can be used on the left side of the equal sign)
# v[3] will be used
# in case of
float me(int i)
# a float will be returned which cannot be used as an lvalue (cannot be set any more), 
# because there is no variable or anything similar in use, just the value of that variable returned.
 
1 members found this post helpful.
Old 05-11-2015, 06:40 AM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Put simply, lvalue is an expression that can appear on the left hand side of an assignment. That is, in C++, an expression you can meaningfully take an address of.
So,
Code:
float me(int l) {
    return v[l];
}

...

me(3)=99;
doesn't make much sense. You cannot assign to a value returned by function. However, if you return the value by reference (and you can think of a reference as a disguised pointer), you get an expression you can take an address of because a reference refers to a valid object you can assign to. It would work with pointers, too:
Code:
float *me(int l) {
    return &v[l];
}

...

*me(3)=99;
In a certain sense, you don't assign to the value returned by the function, but rather to a variable pointed to by the value.
The later standards complicate this even further with so called rvalue references.
 
1 members found this post helpful.
Old 05-11-2015, 07:36 AM   #5
JeremyBoden
Senior Member
 
Registered: Nov 2011
Distribution: Debian
Posts: 1,131

Rep: Reputation: 233Reputation: 233Reputation: 233
C uses call by value - not call by reference.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Error in program, lvalue required as left operand of assignment srinietrx Programming 1 11-13-2014 06:10 AM
[SOLVED] lvalue required as left operand of assignment Ramurd Programming 6 02-04-2011 04:35 AM
invalid lvalue in assignment cdw9997 Programming 9 05-05-2009 06:16 PM
error: invalid lvalue in assignment nasim751 Programming 3 04-10-2008 01:59 PM
Error: invalid lvalue in assignment xxrsc Linux - Software 1 08-17-2006 01:43 PM


All times are GMT -5. The time now is 01:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration