C++ : cannot access node value

标签 c++

我正在尝试使用取消引用运算符打印新创建的节点。

我的主要功能

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 ;

可能的解决方案:

  1. 就像@user4581301 说的,你可以 write your own operator<<适合您的类型 Node .

  2. 您还可以替换 cout << *temp ;声明 cout << temp->data ;cout << (*temp).data ;因为 temp 是指向结构的指针。您可以使用和 -> 访问它在 data 之前或者用 .但在使用 * 取消引用后运营商。

关于C++ : cannot access node value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45451000/

相关文章:

c++ - 做时间戳的最好方法是什么?

c++ - Linux。不打印到文件

c++ - Xcode5 的 MPI 问题

c++ - 如何用新行设置 QLineEdit

c++ - 英特尔 TBB : pool of graphs

c++ - OpenGL:我可以将 glBindBuffer 与 glBindBufferARB 混合使用吗?

C++ zLib 压缩字节数组

c++ - C++从磁盘读取数据

c++ - 使用我的数组类时出现奇怪的警告

c++ - 在滚动窗口上应用矩阵乘法的最聪明方法