1

I am working on very old C++ project. When building my C++ code in Visual Studio 6 I am getting error "cannot convert from 'unsigned short [9]' to 'char []'".

for code below:

char    variable[] = _T("something");

I googled and tried to understand the issue. It could be the way I am building it.

I've already tried the step written in https://mihai-nita.net/2006/07/23/visual-studio-unicode-projects/ (adding UNICODE & _UNICODE in preprocessor). It would be very helpful if someone can suggest a solution to correct this error.

Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
I_AM_VAIBH
  • 67
  • 5
  • 1
    Looks like `_T` is feeding you a wider-than `char` character. Either go all Unicode or don't use any Unicode. That said, it's 2018. Use the Unicode. – user4581301 May 14 '18 at 20:23

1 Answers1

3

The _T macro expands to either a narrow or wide string literal depending on the state of the UNICODE macro. If UNICODE isn't defined _T will expand to a char string, and if UNICODE is defined _T will expand to a wide char string.

If you're using _T, you should use the type TCHAR, which will similarly expand to either char or wchar_t depending on the state of the UNICODE macros:

TCHAR variable[] = _T("something");

This will expand to

char variable[] = "something";

if UNICODE is not defined, and will expand to something like

wchar_t variable[] = L"something";

if UNICODE is defined.

It's worth noting that wchar_t wasn't a real type in early versions of Visual C++, but rather just a typedef for (or macro expanding to) unsigned short, which is why the error message you're seeing references that instead of wchar_t.

Miles Budnek
  • 28,216
  • 2
  • 35
  • 52