cout a string gets the address instead of value [duplicate]

自作多情 提交于 2019-11-27 08:52:16

问题


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;


来源:https://stackoverflow.com/questions/24007507/cout-a-string-gets-the-address-instead-of-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!