WCHAR Problem [DX9, C++]

Started by
2 comments, last by Jan Birsa 15 years, 4 months ago
Hello guys, Lets say I have a WCHAR variable which contains a location of 3D Model: WCHAR g_Model[MAX_PATH] = L"somemodel.x"; Now, In application it happens that I want to change the model from somemodel.x to someothermodel.x I use the case, where I try to change g_Model variable to handle "someothermodel.x", but I get error: error C2440: '=' : cannot convert from 'const wchar_t [14]' to 'WCHAR' So, I made new WCHAR called g_Model2 which contains someothermodel.x, and I tried: g_Model = g_Model2 Compiles well, but application crushes (yes, I have tried setting that device does re-set after this change). Debugger shows tons of stupid errors. BTW: I have tried changing WCHAR to something else, but didnt work out.
Advertisement
Quote:Original post by Jan Birsa
Hello guys,

Lets say I have a WCHAR variable which contains a location of 3D Model:
WCHAR g_Model[MAX_PATH] = L"somemodel.x";

Now, In application it happens that I want to change the model from somemodel.x to someothermodel.x

I use the case, where I try to change g_Model variable to handle "someothermodel.x", but I get error:
error C2440: '=' : cannot convert from 'const wchar_t [14]' to 'WCHAR'
You're trying to write a single wide-character string into a single wide character, which isn't going to work. Can we see your exact code?

You probably want to use wcscpy:
wcscpy(g_Model, L"someothermodel.x");

EDIT: Actually, you'd be better off using std::wstring.

[Edited by - Evil Steve on November 28, 2008 9:30:40 AM]
Its hard to tell you specifically what problem you are having without seeing some concrete code, but two points:

- Are you overloading the assignment operator on the model class?

- It seems as though you are simply copying the raw pointer from the one model class into the other one which may be why you are getting runtime errors. If the containing model object goes out of scope or is released with a delete operator that pointer is no longer valid, and regardless of that you want the new object to contain its own local copy of that wchar array unless you are very careful and know exactly what you are doing, but generally a local copy is the safe bet here. You can use a simply strncpy/strcpy variant for wchar types such as can be found on msdn. Or you can wrap it in a wstring which would actually make your life a little easier.

The point here is that you don't want to do shallow copying of member when any of them contain handles or pointers etc. In your case just write a copy constructor and assignment operator even if both contain duplicate code.
Quote:Original post by Evil Steve
Quote:Original post by Jan Birsa
Hello guys,

Lets say I have a WCHAR variable which contains a location of 3D Model:
WCHAR g_Model[MAX_PATH] = L"somemodel.x";

Now, In application it happens that I want to change the model from somemodel.x to someothermodel.x

I use the case, where I try to change g_Model variable to handle "someothermodel.x", but I get error:
error C2440: '=' : cannot convert from 'const wchar_t [14]' to 'WCHAR'
You're trying to write a single wide-character string into a single wide character, which isn't going to work. Can we see your exact code?

You probably want to use wcscpy:
wcscpy(g_Model, L"someothermodel.x");

EDIT: Actually, you'd be better off using std::wstring.


Thank you so much....wcscpy did the job exellent!

This topic is closed to new replies.

Advertisement