struct Node p=Node(1)和struct Node*b=new Node(1)具体有啥区别?为啥输出的时候,会报错?

代码

#include<iostream>

using namespace std;

int main()
{
    struct Node
    {
        int val;
        Node * next;
        Node (int _val):val(_val),next(NULL){}
    };
    
    struct Node p=Node(1);
    struct Node *b=new Node(1);
    
    cout<<*p<<endl;
    cout<<p<<endl;
    
    cout<<*b<<endl;
    cout<<b<<endl;
    
    return 0;
}


cout<<。。。。
cout是一个ostream类实例
<<是他的重载操作符函数
cout<<相当于 operator<<(const ostream& out,const T&) T 就是你的类,对于 Node 你没有实现operator<<(const ostream& out ,const Node& T)所以出错
而对于Node* ,stl 提供了ostream.operator<<(void* ) Node* 可以直接 转为void* ,所以不报错