Thursday, March 24, 2011

Compilation error when calling _tcsstr and assigning to a wchar_t*

I am getting a compilation error when trying to build a C++ project which previously worked.

The code follows:

const wchar_t* pdest;
pdest = _tcsstr(ConnStr, Name);

The error follows: Error 10 error C2440: '=' : cannot convert from 'const char *' to 'const wchar_t

I'm using Visual Studio 2008. The error message explains the problem well, but I know this program used to compile, what am I doing wrong?

From stackoverflow
  • Your code is dangerous. _tcsstr is a TCHAR macro, so it's definition can change depending on whether or not UNICODE is defined. wchar_t is fixed. The error you're seeing is due to this exact problem - the environment is using the single-byte version of _tcsstr (likely becasue UNICODE is not defined).

    Don't just define UNICODE. Fix the code first. Either use TCHAR macros for both, or the wide character functions.

    Stimy : I was compiling in debug mode instead of a unicode mode which is why this worked previously, but not now. Thanks for your help.
  • _tcsstr is for use with TCHAR. Depending on compile settings, this is either char or wchar_t.

    So either use TCHAR, or wcsstr

0 comments:

Post a Comment