Вопрос

This code:

#include <tchar.h>

TCHAR example_function() {
  TCHAR example_tchar[10];
  return example_tchar;
}

int main() { }

Gives error:

In function 'TCHAR example_function()':
error: invalid conversion from 'TCHAR* {aka char*}' to 'TCHAR {aka char}' [-fpermissive]
warning: address of local variable 'example_tchar' returned [enabled by default]
Это было полезно?

Решение

Your variable example_tchar is not a TCHAR, but an array.

What are you actually trying to achieve?

If you want to return a single TCHAR:

TCHAR example_function() {
  TCHAR example_tchar[10];
  return example_tchar[0];
}
//or simply
TCHAR example_function() {
  TCHAR example_tchar = _T('');
  return example_tchar;
}

If you want to return an array, or rather a pointer, you'll have to dynamically allocate memory to prevent undefined behavior:

TCHAR* example_function() {
  TCHAR* example_tchar = new TCHAR[10];
  return example_tchar;
}

Другие советы

And if you want to return a string of TCHARs use something more complex, like std::basic_string<TCHAR> for example (or use pointers, or just add set TCHAR[10] as return parameter). TCHAR is just a define that selects between char and wchar_t based on build options.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top