By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,495 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,495 IT Pros & Developers. It's quick & easy.

error C2061: syntax error : identifier 'ref'

P: n/a
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.
Jun 27 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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<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????
I think first important step in the troubleshooting is
to decide on whether you are coding C++ or C# !

Arne
Jun 27 '08 #2

P: n/a
On Apr 21, 6:01*am, Arne Vajhøj <a...@vajhoej.dkwrote:
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<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????

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.
Jun 27 '08 #3

P: n/a
On Mon, 21 Apr 2008 09:14:53 -0700, Pixel.to.life
<pi***********@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
Jun 27 '08 #4

P: n/a
Peter Duniho wrote:
On Mon, 21 Apr 2008 09:14:53 -0700, Pixel.to.life
<pi***********@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

Jun 27 '08 #5

P: n/a
On Mon, 21 Apr 2008 10:39:17 -0700, Ben Voigt [C++ MVP]
<rb*@nospam.nospamwrote:
[...]
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
Jun 27 '08 #6

P: n/a
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<String^^args)
{
String^ s = "ABC";
Console::WriteLine(s);
f1(s);
Console::WriteLine(s);
f2(s);
Console::WriteLine(s);
return 0;
}
Arne
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.