【发布时间】:2020-11-05 11:23:52
【问题描述】:
运行时出现错误?
cout << *head << endl;
为什么我们不能取消引用对象指针?
就像我们在 int 数据类型中所做的那样:
int obj = 10;
int *ptr = &obj;
cout << *ptr << endl; //This will print the value 10 by dereferencing the operator!
但为什么不在对象中?
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node(int data)
{
this->data = data;
this->next = NULL;
}
};
int main() {
Node N1(10);
Node *head = &N1;
cout << &N1 << endl;
cout << head << endl;
cout << *head << endl;
cout << &head << endl;
}
【问题讨论】:
-
*head是N1对象,你没有任何方法可以输出Node对象(operator<<没有重载的函数Node)。 -
是否有任何输出语句按预期工作?如果不是,那么您的假设可能与您的问题主题有关是错误的。请注意,在 C++ 中,没有像其他语言那样的对象的默认输出操作。作为这里的新用户,也请带上tour并阅读How to Ask。
标签: c++ object pointers linked-list dereference