error: initial value of reference to non-const must be an lvalue

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

error: initial value of reference to non-const must be an lvalue

 
0
  #1
Jun 11th, 2008
I have a function Scan.MakeLinearGrid() which returns a vector<vector<Vector3> >

I have another function that is made to accept this type of thing:
Scan.setAngleList(vector<vector<Vector3> >);

But if I call it like this:
  1. Scan.setAngleList(Scan.MakeLinearGrid());
it says
  1. error: initial value of reference to non-const must be an lvalue
  2. Scan.setAngleList(Scan.MakeLinearGrid());

but if i do
  1. vector<vector<Vector3> > temp = Scan.MakeLinearGrid();
  2. Scan.setAngleList(temp);

it works fine.

Can someone explain the difference?

Thanks,
Dave
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: error: initial value of reference to non-const must be an lvalue

 
0
  #2
Jun 11th, 2008
Your Vector3 is missing the appropriate copy constructor.

A good rule of thumb is if you use any one of copy constructor, assignment operator, or virtual destructor, then you need all three. (Not always, but usually.)

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: error: initial value of reference to non-const must be an lvalue

 
0
  #3
Jun 11th, 2008
I have neither an assignment operator or virtual destructor, so then why do i need a copy constructor?

Can you give a simple example of each of those?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: error: initial value of reference to non-const must be an lvalue

 
0
  #4
Jun 12th, 2008
What does setAngleList do? When you say Scan.setAngleList(Scan.MakeLinearGrid()); it passes a temporary object to the method. If setAngleList doesn't modify the object, you should make it a const reference anyway because that's both safer and more flexible in what you can pass. If setAngleList does modify the object, you shouldn't be passing a temporary at all, this call has a logical error that's luckily being caught by the compiler, and the working example you gave is the way to fix the error.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: error: initial value of reference to non-const must be an lvalue

 
0
  #5
Jun 12th, 2008
Thanks Ed. I'm glad you've got such a sharp eye. I didn't even think of that...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC