英文:
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 <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;
}
答案1
得分: 4
在你的代码中有两个问题:
- 你尝试使用
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;
至于第二个问题:
- 你不能使用
<<
将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 << "}";
}
};
参考链接:
- https://stackoverflow.com/q/4421706/5740428
- https://stackoverflow.com/q/7665021/5740428
- https://stackoverflow.com/q/56795676/5740428
英文:
There are two issues in your code:
> cpp
> // If I replace this line with int x = *(nodeA->next) it gives me error saying:
> // no suitable function conversion from node to int exists
> node x = *(nodeA->next);
>
Your attempt with int
fails because *(nodeA->next)
is a node&
, 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& () { 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->next.data;
// or
node* nextNode = nodeA->next;
As for the second issue:
> cpp
> cout << "ans: " << x <<endl;
>
You cannot insert x
into the std::cout
stream with <<
, because it has no operator overload that does this. Either print just the data with << 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<< as "hidden friend"
friend operator<<(std::ostream& out, const node& n) {
return out << "node{" << n.data << "}";
}
};
See also:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论