cout a string gets the address instead of value [d

2019-03-06 23:25发布

问题:

This question already has an answer here:

  • How I can print the wchar_t values to console? 7 answers

There is a macro defined as below:

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

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

TCHAR* test = TEXT("test");
cout << 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!

回答1:

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

and then:

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


标签: c++ macros cout