cout 字符串获取地址而不是值 [英] cout a string gets the address instead of value

查看:24
本文介绍了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;

这篇关于cout 字符串获取地址而不是值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆