error C2061: syntax error : identifier 'ref'

  • Thread starter Thread starter Pixel.to.life
  • Start date Start date
P

Pixel.to.life

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<typename T> ref class ManagedImageModifier
{
public:
...
...

bool ChangeImage(Bitmap^ 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.
 
Pixel.to.life said:
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<typename T> ref class ManagedImageModifier
{
public:
...
...

bool ChangeImage(Bitmap^ 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
 
Pixel.to.life said:
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<typename T> ref class ManagedImageModifier
{
   public:
       ...
       ...
       bool ChangeImage(Bitmap^ 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.
 
Peter said:
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".
 
Pixel.to.life said:
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<String^> ^args)
{
String^ s = "ABC";
Console::WriteLine(s);
f1(s);
Console::WriteLine(s);
f2(s);
Console::WriteLine(s);
return 0;
}


Arne
 

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

Back
Top