error C2061: syntax error : identifier 'ref'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pixel.to.life

    error C2061: syntax error : identifier 'ref'

    So I have this perfectly fine and running app, that uses managed C++
    forms.

    Problem#1:

    [1] I pass a Bitmap reference to a class, hoping to modify it in one
    of the class's methods, so it reflects outside too. Something like
    this:


    // In a form's scope
    Bitmap ^m_Bitmap;


    // A separate class
    template<typena me Tref class ManagedImageMod ifier
    {
    public:
    ...
    ...

    bool ChangeImage(Bit map^ iImage)
    {
    // change iImage here
    ....
    return true;
    };


    };


    This builds fine. The problem is that iImage has a different address
    in memory than the reference I pass in. Obviously this means any
    change to iImage isnt reflected outside. This came as a surprise
    initially to me as I am new to managed programming.


    Problem#2:
    Anyways, I chose to classify this parameter as a reference variable,
    by using 'ref' keyword. Something like this

    bool ChangeImage(ref Bitmap^ iImage)
    {
    // change iImage here
    ....
    return true;
    };


    And now I get this compile error:

    error C2061: syntax error : identifier 'ref'

    Note that the same keyword when used to classify the class
    ImageModifier wasnt giving me errors.

    Any clues on whats going on here????

    Thanks a lot!

    -P.


  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

    #2
    Re: error C2061: syntax error : identifier 'ref'

    Pixel.to.life wrote:
    So I have this perfectly fine and running app, that uses managed C++
    forms.
    >
    Problem#1:
    >
    [1] I pass a Bitmap reference to a class, hoping to modify it in one
    of the class's methods, so it reflects outside too. Something like
    this:
    >
    // In a form's scope
    Bitmap ^m_Bitmap;
    >
    // A separate class
    template<typena me Tref class ManagedImageMod ifier
    {
    public:
    ...
    ...
    >
    bool ChangeImage(Bit map^ iImage)
    {
    // change iImage here
    ....
    return true;
    };
    };
    >
    This builds fine. The problem is that iImage has a different address
    in memory than the reference I pass in. Obviously this means any
    change to iImage isnt reflected outside. This came as a surprise
    initially to me as I am new to managed programming.
    >
    Problem#2:
    Anyways, I chose to classify this parameter as a reference variable,
    by using 'ref' keyword. Something like this
    >
    bool ChangeImage(ref Bitmap^ iImage)
    {
    // change iImage here
    ....
    return true;
    };
    >
    >
    And now I get this compile error:
    >
    error C2061: syntax error : identifier 'ref'
    >
    Note that the same keyword when used to classify the class
    ImageModifier wasnt giving me errors.
    >
    Any clues on whats going on here????
    I think first important step in the troubleshooting is
    to decide on whether you are coding C++ or C# !

    Arne

    Comment

    • Pixel.to.life

      #3
      Re: error C2061: syntax error : identifier 'ref'

      On Apr 21, 6:01 am, Arne Vajhøj <a...@vajhoej.d kwrote:
      Pixel.to.life wrote:
      So I have this perfectly fine and running app, that uses managed C++
      forms.
      >
      Problem#1:
      >
      [1] I pass a Bitmap reference to a class, hoping to modify it in one
      of the class's methods, so it reflects outside too. Something like
      this:
      >
      // In a form's scope
      Bitmap     ^m_Bitmap;
      >
      // A separate class
      template<typena me Tref class ManagedImageMod ifier
      {
         public:
             ...
             ...
      >
             bool ChangeImage(Bit map^ iImage)
             {
                // change iImage here
                ....
                return true;
             };
      };
      >
      This builds fine. The problem is that iImage has a different address
      in memory than the reference I pass in. Obviously this means any
      change to iImage isnt reflected outside. This came as a surprise
      initially to me as I am new to managed programming.
      >
      Problem#2:
      Anyways, I chose to classify this parameter as a reference variable,
      by using 'ref' keyword. Something like this
      >
      bool ChangeImage(ref Bitmap^ iImage)
             {
                // change iImage here
                ....
                return true;
             };
      >
      And now I get this compile error:
      >
      error C2061: syntax error : identifier 'ref'
      >
      Note that the same keyword when used to classify the class
      ImageModifier wasnt giving me errors.
      >
      Any clues on whats going on here????
      >
      I think first important step in the troubleshooting is
      to decide on whether you are coding C++ or C# !
      >
      Arne- Hide quoted text -
      >
      - Show quoted text -
      It is managed C++.
      I already got a solution on the VC group, doesnt quite work. That
      solution recommended using a '%' qualifier instead of
      a 'ref' keyword to pass by reference. It builds fine, but still same
      issue: what is passed to the method is a different reference from what
      is intended.

      Since the problem spans a bit of both C++/CLI, I posted it here too.

      Thanks anyways.

      Comment

      • Peter Duniho

        #4
        Re: error C2061: syntax error : identifier 'ref'

        On Mon, 21 Apr 2008 09:14:53 -0700, Pixel.to.life
        <pixel.to.life@ gmail.comwrote:
        It is managed C++.
        Then you're posting to the wrong newsgroup. It does seem as though you
        want to pass by reference. And if you were writing C# code, the "ref"
        keyword would be useful for that.

        But you're not. In C++ you can use the '&' character (not '%' AFAIK) and
        it should work. If it doesn't, then you'll need to post your question to
        a newsgroup specific to C++, not a newsgroup specific to C#.

        Pete

        Comment

        • Ben Voigt [C++ MVP]

          #5
          Re: error C2061: syntax error : identifier 'ref'

          Peter Duniho wrote:
          On Mon, 21 Apr 2008 09:14:53 -0700, Pixel.to.life
          <pixel.to.life@ gmail.comwrote:
          >
          >It is managed C++.
          >
          Then you're posting to the wrong newsgroup. It does seem as though
          you want to pass by reference. And if you were writing C# code, the
          "ref" keyword would be useful for that.
          >
          But you're not. In C++ you can use the '&' character (not '%' AFAIK)
          and it should work. If it doesn't, then you'll need to post your
          question to a newsgroup specific to C++, not a newsgroup specific to
          C#.
          & is standard C++ for a reference.
          Standard C++ pointers and references don't play well with a compacting
          garbage collector such as .NET has, because objects move around in memory.
          So C++/CLI adds a tracking handle (^) and tracking reference (%) which are
          GC-aware. The tracking reference is how you define a parameter which C#
          sees as "ref" or "out".
          >
          Pete

          Comment

          • Peter Duniho

            #6
            Re: error C2061: syntax error : identifier 'ref'

            On Mon, 21 Apr 2008 10:39:17 -0700, Ben Voigt [C++ MVP]
            <rbv@nospam.nos pamwrote:
            [...]
            So C++/CLI adds a tracking handle (^) and tracking reference (%) which
            are
            GC-aware. The tracking reference is how you define a parameter which C#
            sees as "ref" or "out".
            Ah, okay.

            Well, there's a good example of why this question doesn't belong in this
            newsgroup. :)

            To the OP: if you can't get % to work for you, you need to post your
            question in a managed C++ newsgroup. It has nothing to do with C#. I
            recommend when you do so, that you include a concise-but-complete code
            sample that reliably demonstrates the problem.

            Pete

            Comment

            • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

              #7
              Re: error C2061: syntax error : identifier 'ref'

              Pixel.to.life wrote:
              It is managed C++.
              I already got a solution on the VC group, doesnt quite work. That
              solution recommended using a '%' qualifier instead of
              a 'ref' keyword to pass by reference. It builds fine, but still same
              issue: what is passed to the method is a different reference from what
              is intended.
              >
              Since the problem spans a bit of both C++/CLI, I posted it here too.
              This group has nothing to with C++ or C++/CLI.

              It is easy to show that % is working.

              Simple example:

              #using <mscorlib.dll >

              using namespace System;

              void f1(String^ s)
              {
              s = "f1";
              }

              void f2(String^ %s)
              {
              s = "f2";
              }

              int main(array<Stri ng^^args)
              {
              String^ s = "ABC";
              Console::WriteL ine(s);
              f1(s);
              Console::WriteL ine(s);
              f2(s);
              Console::WriteL ine(s);
              return 0;
              }


              Arne

              Comment

              Working...