为什么在链表中我不能解引用下一个指针来打印下一个节点的值?

huangapple go评论67阅读模式
英文:

In a linked list, why cant I dereference the next pointer to print the value of the next node

问题

I am learning c++ for the first time, In a basic linked list if node* next is a simple pointer then if I dereference it, It should give me the value at the next node but it doesnt, instead I have to access it through int data. I just want to know why it does that in a linked list and not when I dereference it for a single variable say int x=5.

Heres, the code

#include <iostream>;

using namespace std;

class node {
public:
    int data;
    node* next;

    node(int data){
        this->data = data;
    }

};

int main(){

    node* nodeA = new node(25);
    node* nodeB = new node(30);
    node* nodeC = new node(40);

    nodeA->next = nodeB;
    nodeB->next = nodeC;
    node x = *(nodeA->next); /* If I replace this line with int x = *(nodeA->next) it gives me error saying - no suitable function conversion from node to int exists */

    cout<<"node A: "<< nodeA->data << " : " << (*nodeA->next).data <<endl;
    cout<<"nodeB: "<< &nodeB->data << " : "<< nodeB->next <<endl;

    cout<<"ans: "<< x <<endl;   // ERROR HERE SAYS - NO OPERATOR "<<" MATCHES THESE OPERANDS

    return 0;
}

(Note: I fixed the code formatting for you.)

英文:

I am learning c++ for the first time, In a basic linked list if node* next is a simple pointer then if I dereference it, It should give me the value at the next node but it doesnt, instead I have to access it through int data. I just want to know why it does that in a linked list and not when I dereference it for a single variable say int x=5.

Heres, the code

#include &lt;iostream&gt;


using namespace std;


class node {
public:
    int data;
    node* next;

    
    node(int data){
        this-&gt;data = data;
    }

};

int main(){

    node* nodeA = new node(25);
    node* nodeB = new node(30);
    node* nodeC = new node(40);

    nodeA-&gt;next = nodeB;
    nodeB-&gt;next = nodeC;
    node x = *(nodeA-&gt;next); /* If I replace this line with int x = *(nodeA-&gt;next) it gives me error saying - no suitable function conversion from node to int exists */

    cout&lt;&lt;&quot;node A: &quot;&lt;&lt; nodeA-&gt;data &lt;&lt;&quot; : &quot; &lt;&lt; (*nodeA-&gt;next).data &lt;&lt;endl;
    cout&lt;&lt;&quot;nodeB: &quot;&lt;&lt; &amp;nodeB-&gt;data&lt;&lt;&quot; : &quot;&lt;&lt; nodeB-&gt;next &lt;&lt;endl;

    cout&lt;&lt;&quot;ans: &quot;&lt;&lt; x &lt;&lt;endl;   // ERROR HERE SAYS - NO OPERATOR &quot;&lt;&lt;&quot; MATCHES THESE OPERANDS

    return 0;
}

答案1

得分: 4

在你的代码中有两个问题:

  1. 你尝试使用 int 失败,因为 *(nodeA->next) 是一个 node&,而 int x 是一个 int。一个 node 不能隐式转换为 int(除非你为其添加转换函数),所以你会得到一个错误。用户定义的隐式转换函数可以像这样:
class node {
public:
    // ...
    operator int& () { return data; }
    operator int () const { return data; }
};

目前像你当前这样复制整个链表节点也是不寻常的,你可以这样做:

int nextValue = nodeA->next.data;
// 或者
node* nextNode = nodeA->next;

至于第二个问题:

  1. 你不能使用 <<x 插入到 std::cout 流中,因为它没有用于此操作的运算符重载。要么只打印数据,如 << x.data,要么为节点创建一个运算符重载:
class node {
public:
    int data;
    node* next;

    // 成员初始化列表
    node(int data) : data{data}, next{nullptr} {}

    // 将 operator<< 定义为“隐藏友元”
    friend operator<<(std::ostream& out, const node& n) {
        return out << "node{" << n.data << "}";
    }
};

参考链接:

英文:

There are two issues in your code:
> cpp
&gt; // If I replace this line with int x = *(nodeA-&gt;next) it gives me error saying:
&gt; // no suitable function conversion from node to int exists
&gt; node x = *(nodeA-&gt;next);
&gt;

Your attempt with int fails because *(nodeA-&gt;next) is a node&amp;, but int x is an int. A node can't be implicitly converted to int (unless you add conversion functions for it), so you get an error. The user-defined implicit conversion functions could look like this:

class node {
public:
    //...
    operator int&amp; () { return data; }
    operator int () const { return data; }
};

Copying the whole linked list node like you're doing currently is also unusual, so you would rather:

int nextValue = nodeA-&gt;next.data;
// or
node* nextNode = nodeA-&gt;next;

As for the second issue:

> cpp
&gt; cout &lt;&lt; &quot;ans: &quot; &lt;&lt; x &lt;&lt;endl;
&gt;

You cannot insert x into the std::cout stream with &lt;&lt;, because it has no operator overload that does this. Either print just the data with &lt;&lt; x.data, or create an operator overload for a node:

class node {
public:
    int data;
    node* next;

    // member initializer list
    node(int data) : data{data}, next{nullptr} {}

    // define operator&lt;&lt; as &quot;hidden friend&quot;
    friend operator&lt;&lt;(std::ostream&amp; out, const node&amp; n) {
        return out &lt;&lt; &quot;node{&quot; &lt;&lt; n.data &lt;&lt; &quot;}&quot;;
    }
};

See also:

huangapple
  • 本文由 发表于 2023年6月15日 17:37:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定