Class, char array, error

M

MX

Welcome!

I have something like that:

ref class DCAM
{
...
private:
char str[64];
...

};

And in main program:

DCAM^ CAM = gcnew DCAM();
CAM->str[0] = 'a';
this->Text = gcnew System::String(Cam->str);

After compilation I got:
error C2664: 'System::String::String(const wchar_t *)' : cannot convert
parameter 1 from 'char [64]' to 'const wchar_t *'
1> Cannot convert a managed type to an unmanaged type

Thanks for any help.
 
A

Arnaud Debaene

MX said:
Welcome!

I have something like that:

ref class DCAM
{
...
private:
char str[64];
...

};

And in main program:

DCAM^ CAM = gcnew DCAM();
CAM->str[0] = 'a';
this->Text = gcnew System::String(Cam->str);

After compilation I got:
error C2664: 'System::String::String(const wchar_t *)' : cannot
convert parameter 1 from 'char [64]' to 'const wchar_t *'
1> Cannot convert a managed type to an unmanaged type

Thanks for any help.

Managed code always work in Unicode, so you can't directly convert from char
to String. Use System::Runtime::Interop::Marshal::ptrToStringAnsi.

Arnaud
MVP - VC
 
H

Holger Grund

Arnaud Debaene said:
Managed code always work in Unicode, so you can't directly convert from
char to String. Use System::Runtime::Interop::Marshal::ptrToStringAnsi.
It's probably also worth mentioning that C++'s char translates
to System::Byte and the C++ alias for System::Char is
wchar_t.

-hg
 

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

Top