Error : Formal argument requires an lvalue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gil

    Error : Formal argument requires an lvalue

    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::stor e(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.
  • Phlip

    #2
    Re: Error : Formal argument requires an lvalue

    Gil wrote:
    [color=blue]
    > 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::stor e(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.[/color]

    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.

    --
    Phlip




    Comment

    • Sumit Rajan

      #3
      Re: Error : Formal argument requires an lvalue


      "Gil" <brightoceanlig ht@hotmail.com> wrote in message
      news:adc2ca29.0 401300647.13304 207@posting.goo gle.com...[color=blue]
      > 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
      > }[/color]

      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.
      [color=blue]
      > void OtherData::stor e(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.[/color]


      Comment

      • Rob Williscroft

        #4
        Re: Error : Formal argument requires an lvalue

        Gil wrote in news:adc2ca29.0 401300647.13304 207@posting.goo gle.com:
        [color=blue]
        > Can anyone see why I'm getting an error with the following code :
        >
        > void NewData::store( NewDataStruct nd, unsigned long& NewDataID)
        > {[/color]

        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::stor e( nd, NewDataID );
        [color=blue]
        > OtherData.store (nd, NewDataID); // error in this line
        > // error with NewDataID
        > }
        >
        > void OtherData::stor e(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.[/color]

        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.
        --

        Comment

        • Karl Heinz Buchegger

          #5
          Re: Error : Formal argument requires an lvalue

          Phlip wrote:[color=blue]
          >
          > Gil wrote:
          >[color=green]
          > > 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::stor e(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.[/color]
          >
          > 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?[/color]

          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.

          --
          Karl Heinz Buchegger
          kbuchegg@gascad .at

          Comment

          • Peter Koch Larsen

            #6
            Re: Error : Formal argument requires an lvalue


            "Gil" <brightoceanlig ht@hotmail.com> skrev i en meddelelse
            news:adc2ca29.0 401300647.13304 207@posting.goo gle.com...[color=blue]
            > 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::stor e(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.[/color]

            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


            Comment

            • Gil

              #7
              Re: Error : Formal argument requires an lvalue

              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::stor e(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.

              Comment

              Working...