gpt4 book ai didi

C++ wstringstream << NULL

转载 作者:太空宇宙 更新时间:2023-11-04 15:49:27 30 4
gpt4 key购买 nike

我在使用 wstringstream 时遇到问题。当我这样做的时候

    std::wstringstream ss;
wchar_t* str = NULL;
ss << str;

应用程序因错误而崩溃

Unhandled exception at 0x53e347af (msvcr100d.dll) in stringstr.exe: 0xC0000005: Access violation reading location 0x00000000.

例如,这很好用:

ss << NULL;
wchar_t* str = L"smth";
ss << &str;

str 并不总是有值(value),有时它可能为 NULL,当它为 NULL 时我想将 0 放入流中。如何解决?

最佳答案

如果为空,则不输出空的wchar_t指针:

( str ? ss << str : ss << 0 );

请注意,这是行不通的:

ss << ( str ? str : 0 )

由于隐式条件运算符的返回类型是其两个表达式的公共(public)类型,因此它仍会返回一个空的 wchar_t 指针。

关于C++ wstringstream << NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10873168/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com