Jump to content
  • Advertisement
Sign in to follow this  
Jan Birsa

WCHAR Problem [DX9, C++]

This topic is 3614 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!