我正在尝试使用取消引用运算符打印新创建的节点。
我的主要功能
int main ()
{
Insert(3);
return 0;
}
Insert() 函数
void Insert(int data)
{
Node *temp = new Node ;
temp -> data = data ;
temp -> next = NULL ;
cout << *temp ;
}
我的错误:
tp.cpp: In function ‘void Insert(int)’:
tp.cpp:27:10: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘Node’)
cout << *temp ;
最佳答案
问题:C++ 仅为其基本类型提供运算符,对于用户定义的类型,我们必须编写自己的运算符。错误 no match for ‘operator<<’
说编译器找不到 <<
运算符反对声明 cout << *temp ;
可能的解决方案:
就像@user4581301 说的,你可以 write your own operator的
<<
适合您的类型Node
.您还可以替换
cout << *temp ;
声明cout << temp->data ;
或cout << (*temp).data ;
因为 temp 是指向结构的指针。您可以使用和->
访问它在data
之前或者用.
但在使用*
取消引用后运营商。
关于C++ : cannot access node value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45451000/