Connecting Tech Pros Worldwide Forums | Help | Site Map

error C2061: syntax error : identifier 'ref'

Pixel.to.life
Guest
 
Posts: n/a
#1: Jun 27 '08
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 Tref 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.



Ben Voigt [C++ MVP]
Guest
 
Posts: n/a
#2: Jun 27 '08

re: error C2061: syntax error : identifier 'ref'


Problem#2:
Quote:
Anyways, I chose to classify this parameter as a reference variable,
by using 'ref' keyword. Something like this
This is the right thing to do, but the wrong syntax.
Quote:
>
>
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????
C# uses the ref keyword for this. C++ already had language syntax for
reference parameters, so C++/CLI adapted that instead of following C#.

Try

bool ChangeImage(Bitmap^% iImage) { ... }

Quote:
>
>
Thanks a lot!
>
>
-P.

Pixel.to.life
Guest
 
Posts: n/a
#3: Jun 27 '08

re: error C2061: syntax error : identifier 'ref'


Thanks Ben.

I tried that, now it compiled file. But the memory address of the
original image and the one I receive inside ChangeImage(..) is still
different. Is there a rul for calling this method too?

I call it like this:

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


ManagedImageModifier<MyModifiermodifier;

bool result = modifier.ChangeImage(m_Bitmap);




On Apr 21, 6:37*am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
Quote:
Quote:
Problem#2:
Anyways, I chose to classify this parameter as a reference variable,
by using 'ref' keyword. Something like this
>
This is the right thing to do, but the wrong syntax.
>
>
>
>
>
>
>
Quote:
bool ChangeImage(ref Bitmap^ iImage)
* * * {
* * * * *// change iImage here
* * * * *....
* * * * *return true;
* * * };
>
Quote:
And now I get this compile error:
>
Quote:
error C2061: syntax error : identifier 'ref'
>
Quote:
Note that the same keyword when used to classify the class
ImageModifier wasnt giving me errors.
>
Quote:
Any clues on whats going on here????
>
C# uses the ref keyword for this. *C++ already had language syntax for
reference parameters, so C++/CLI adapted that instead of following C#.
>
Try
>
bool ChangeImage(Bitmap^% iImage) { ... }
>
>
>
>
>
Quote:
Thanks a lot!
>
Quote:
-P.- Hide quoted text -
>
- Show quoted text -- Hide quoted text -
>
- Show quoted text -
Ben Voigt [C++ MVP]
Guest
 
Posts: n/a
#4: Jun 27 '08

re: error C2061: syntax error : identifier 'ref'


Pixel.to.life wrote:
Quote:
Thanks Ben.
>
I tried that, now it compiled file. But the memory address of the
original image and the one I receive inside ChangeImage(..) is still
different. Is there a rul for calling this method too?
It's a garbage collected object, so it can move around in memory. That's
why ^ and % ("tracking" pointer and reference) instead of * and &.

Can you verify whether the function was able to change the caller's copy of
the variable?
Quote:
>
I call it like this:
>
// In a form's scope
Bitmap ^m_Bitmap;
>
>
ManagedImageModifier<MyModifiermodifier;
>
bool result = modifier.ChangeImage(m_Bitmap);
>
>
>
>
On Apr 21, 6:37 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
Quote:
Quote:
>>Problem#2:
>>Anyways, I chose to classify this parameter as a reference variable,
>>by using 'ref' keyword. Something like this
>>
>This is the right thing to do, but the wrong syntax.
>>
>>
>>
>>
>>
>>
>>
Quote:
>>bool ChangeImage(ref Bitmap^ iImage)
>>{
>>// change iImage here
>>....
>>return true;
>>};
>>
Quote:
>>And now I get this compile error:
>>
Quote:
>>error C2061: syntax error : identifier 'ref'
>>
Quote:
>>Note that the same keyword when used to classify the class
>>ImageModifier wasnt giving me errors.
>>
Quote:
>>Any clues on whats going on here????
>>
>C# uses the ref keyword for this. C++ already had language syntax for
>reference parameters, so C++/CLI adapted that instead of following
>C#.
>>
>Try
>>
>bool ChangeImage(Bitmap^% iImage) { ... }
>>
>>
>>
>>
>>
Quote:
>>Thanks a lot!
>>
Quote:
>>-P.- Hide quoted text -
>>
>- Show quoted text -- Hide quoted text -
>>
>- Show quoted text -

Ben Voigt [C++ MVP]
Guest
 
Posts: n/a
#5: Jun 27 '08

re: error C2061: syntax error : identifier 'ref'


Pixel.to.life wrote:
Quote:
Thanks Ben.
>
I tried that, now it compiled file. But the memory address of the
original image and the one I receive inside ChangeImage(..) is still
different. Is there a rul for calling this method too?
>
I call it like this:
>
// In a form's scope
Bitmap ^m_Bitmap;
>
>
ManagedImageModifier<MyModifiermodifier;
>
bool result = modifier.ChangeImage(m_Bitmap);
Does it still misbehave when simplified?

Try this:

Bitmap ^m_Bitmap;

m_Bitmap = gcnew Bitmap(64, 64);
::System::Diagnostics::Trace::WriteLine("m_Bitmap " + ((m_Bitmap ==
nullptr)? "is": "is not") + " NULL");
ChangeImage(m_Bitmap);
::System::Diagnostics::Trace::WriteLine("m_Bitmap " + ((m_Bitmap ==
nullptr)? "is": "is not") + " NULL");

bool ChangeImage(Bitmap^% bmp) { bmp = nullptr; return true; }

Then move ChangeImage into your template class as a static method, call it
there, test again.
Then make ChangeImage an instance method, test again.
Then start adding the ChangeImage logic.

I suspect that ChangeImage didn't reach the line which reassigned the
parameter to a new value. It is declared with a return value but you aren't
checking it.


Pixel.to.life
Guest
 
Posts: n/a
#6: Jun 27 '08

re: error C2061: syntax error : identifier 'ref'


On Apr 21, 10:43*am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
Quote:
Pixel.to.life wrote:
Quote:
Thanks Ben.
>
Quote:
I tried that, now it compiled file. But the memory address of the
original image and the one I receive inside ChangeImage(..) is still
different. Is there a rul for calling this method too?
>
Quote:
I call it like this:
>
Quote:
// In a form's scope
Bitmap *^m_Bitmap;
>
Quote:
ManagedImageModifier<MyModifiermodifier;
>
Quote:
bool result = modifier.ChangeImage(m_Bitmap);
>
Does it still misbehave when simplified?
>
Try this:
>
Bitmap ^m_Bitmap;
>
m_Bitmap = gcnew Bitmap(64, 64);
::System::Diagnostics::Trace::WriteLine("m_Bitmap " + ((m_Bitmap ==
nullptr)? "is": "is not") + " NULL");
ChangeImage(m_Bitmap);
::System::Diagnostics::Trace::WriteLine("m_Bitmap " + ((m_Bitmap ==
nullptr)? "is": "is not") + " NULL");
>
bool ChangeImage(Bitmap^% bmp) { bmp = nullptr; return true; }
>
Then move ChangeImage into your template class as a static method, call it
there, test again.
Then make ChangeImage an instance method, test again.
Then start adding the ChangeImage logic.
>
I suspect that ChangeImage didn't reach the line which reassigned the
parameter to a new value. *It is declared with a return value but you aren't
checking it.- Hide quoted text -
>
- Show quoted text -
Ben,

That worked like a charm.

Thanks a ton. I was stuck on this for almost a day!!!

Hope I can be of some use to you too someday:-)
Ben Voigt [C++ MVP]
Guest
 
Posts: n/a
#7: Jun 27 '08

re: error C2061: syntax error : identifier 'ref'


Ben,
Quote:
>
That worked like a charm.
>
Thanks a ton. I was stuck on this for almost a day!!!
>
Hope I can be of some use to you too someday:-)
You're welcome.


Closed Thread