Error : Formal argument requires an lvalue

G

Gil

Can anyone see why I'm getting an error with the following code :

void NewData::store(NewDataStruct nd, unsigned long& NewDataID)
{
OtherData.store(nd, NewDataID); // error in this line
// error with NewDataID
}

void OtherData::store(NewDataStruct nd, unsigned long NewDataID)
{

..
..
..

}

error message is :
"src/DataObjects.cpp", line 30: Error: Formal argument _NewDataID of
type unsigned& in call to NewData::store(const NewDataStruct&,
unsigned&) requires an lvalue.
 
P

Phlip

Gil said:
Can anyone see why I'm getting an error with the following code :

void NewData::store(NewDataStruct nd, unsigned long& NewDataID)
{
OtherData.store(nd, NewDataID); // error in this line
// error with NewDataID
}

void OtherData::store(NewDataStruct nd, unsigned long NewDataID)
{

.
.
.

}

error message is :
"src/DataObjects.cpp", line 30: Error: Formal argument _NewDataID of
type unsigned& in call to NewData::store(const NewDataStruct&,
unsigned&) requires an lvalue.

How is your compiler expected to guess which version of store() to call
where? If you feed it things which could be used as unsigned long& or
unsigned long, how does it know which version to call?

Write only one version of the function, with either unsigned long& - meaning
you intend to change the argument, or unsigned long, meaning you don't.
 
S

Sumit Rajan

Gil said:
Can anyone see why I'm getting an error with the following code :

void NewData::store(NewDataStruct nd, unsigned long& NewDataID)
{
OtherData.store(nd, NewDataID); // error in this line
// error with NewDataID
}

You need to decide whether you want NewDataID to be a const unsigned long&.

The error could have occurred due to a situation like this:

int test(int& i) //error:needs to be const int& in such a case
{
return i+1;
}

int main()
{
test(2);
}

If you do need to change the value of the integer:

int test(int& i)
{
i=100;
return i;
}

int main()
{
int j=34;
test(j);
}


Regards,
Sumit.
 
R

Rob Williscroft

Gil wrote in
Can anyone see why I'm getting an error with the following code :

void NewData::store(NewDataStruct nd, unsigned long& NewDataID)
{

Whats OtherData here, you use it like its an variable identifier
but later I see its a class name.

Did you mean to write:

OtherData::store( nd, NewDataID );
OtherData.store(nd, NewDataID); // error in this line
// error with NewDataID
}

void OtherData::store(NewDataStruct nd, unsigned long NewDataID)
{

.
.
.

}

error message is :
"src/DataObjects.cpp", line 30: Error: Formal argument _NewDataID of
type unsigned& in call to NewData::store(const NewDataStruct&,
unsigned&) requires an lvalue.

The above message contains:

NewData::store(const NewDataStruct&, unsigned&);

Which is *not* a declaration you've shown us. It might help
if you copy (cut & paste) the actual code into your message, then
remove the unnessasery details.

Rob.
 
K

Karl Heinz Buchegger

Phlip said:
How is your compiler expected to guess which version of store() to call
where? If you feed it things which could be used as unsigned long& or
unsigned long, how does it know which version to call?

You may have missed, that both store functions belong to different
classes acoriding to the posted code.

On the other hand, the posted error message doesn't fit with that.
More info from the OP is needed.
 
P

Peter Koch Larsen

Gil said:
Can anyone see why I'm getting an error with the following code :

void NewData::store(NewDataStruct nd, unsigned long& NewDataID)
{
OtherData.store(nd, NewDataID); // error in this line
// error with NewDataID
}

void OtherData::store(NewDataStruct nd, unsigned long NewDataID)
{

.
.
.

}

error message is :
"src/DataObjects.cpp", line 30: Error: Formal argument _NewDataID of
type unsigned& in call to NewData::store(const NewDataStruct&,
unsigned&) requires an lvalue.

The code above is okay. You must have made an error when doing cut&paste.
This also explains the error-message, where the signature of NewData::store
differs from that of the example.

Kind regards
Peter
 
G

Gil

First off, Thanks very much every one for all the helpful feedback!!

I changed the code to remove unnecessary details, but I think the code
was changed in the translation.

More accurate :

void NewData::store(NewDataStruct nd, unsigned long& NewDataID)
{
od.store(nd, NewDataID); // error in this line
// error with NewDataID
// od is an instantiation of OtherData
// od is private to NewData
}

void OtherData::store(NewDataStruct nd, unsigned long NewDataID)
{

..
..
..

}

The code in question actually compiles without error on Microsoft
Visual C++ 6.0. But I get the error when compiling with SunC++ 5.4.
At first I thought the error might be because I am passing NewDataID
into NewData::store by reference and then am passing this into
od.store not as a reference. Could that cause the error? NewData and
OtherData are two different classes. I want the parameter NewDataID to
be changed in NewData, but not in OtherData. The error message was
strange in that it didn't seem to fit with the code I had. Maybe I
should put more of the actual code in.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top