无法在屏幕上打印nullptr的值 [英] Unable to print the value of nullptr on screen

查看:362
本文介绍了无法在屏幕上打印nullptr的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读关于 nullptr 并在g ++和VS2010上进行锻炼。

I was reading about nullptr and doing workout on g++ and also on VS2010.

/ p>

When I did

#include <iostream>
using namespace std;

auto main(void)->int
{
    int j{};    
    int* q{};   

    cout << "Value of j: " << j << endl; // prints 0
    cout << nullptr << endl;
    cout << "Value of q: " << q << endl; // prints 0

    return 0;
}

打印 nullptr 在屏幕上,g ++和VS给出编译器错误。
是否允许在屏幕上打印 nullptr 的值?

printing the value of nullptr on screen, g++ and VS gave compiler error. Is it not allowed to print the value of nullptr on screen?

推荐答案

指针字面值是关键字nullptr。它是std :: nullptr_t类型的prvalue。

类型nullptr_t应该可以转换为 T * ,但编译器对于 nullptr_t 没有运算符< <,并且不知道要使用哪种类型转换 nullptr

Type nullptr_t should be convertible to T*, but compiler has no operator << for nullptr_t and don't know to which type you want to convert nullptr.

您可以使用

cout << static_cast<void*>(nullptr) << endl;

这篇关于无法在屏幕上打印nullptr的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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