cout 字符串获取地址而不是值

2022-01-11 00:00:00 macros c++ cout

有一个宏定义如下:

#ifdef UNICODE
typedef wchar_t     TCHAR;
#define TEXT(quote) L##quote
#else
typedef char        TCHAR;
#define TEXT(quote) quote
#endif

当我尝试使用 std::cout 打印消息时,如下所示:

When I try to print a message using std::cout as below:

TCHAR* test = TEXT("test");
cout << test;

我得到的地址是 00D82110 而不是值test".

What I get the address such as 00D82110 instead of the value "test".

任何人都可以提出任何建议,我如何在此处打印值?非常感谢!

Can anybody give any suggestion how can I print the value here? Thanks a lot!

推荐答案

对于宽字符,您需要使用 wcout 而不是 cout.这样做:

You need to use wcout instead of cout for wide characters. Do this:

#ifdef UNICODE
    typedef wchar_t     TCHAR;
    #define TEXT(quote) L##quote
    #define COUT        wcout
#else
    typedef char        TCHAR;
    #define TEXT(quote) quote
    #define COUT        cout
#endif

然后:

TCHAR* test = TEXT("test");
COUT << test;

相关文章