2

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;
}
wohlstad
  • 12,661
  • 10
  • 26
  • 39
Anand
  • 21
  • 1
  • It's declared as `node* next`, so dereferencing `next` you're getting an object with `node` type – Renat Jun 15 '23 at 09:40
  • 2
    "It should give me" What gives you this impression? It should not, and does not. – n. m. could be an AI Jun 15 '23 at 09:41
  • the error: _"ERROR HERE SAYS - NO OPERATOR "<<" MATCHES THESE OPERANDS"_ is because you are trying to print the `node` object using the `<<` operator, if you really want to print it that way (I advise not) you can define your own [overloaded operator](https://stackoverflow.com/questions/476272/how-to-properly-overload-the-operator-for-an-ostream) and print it using `<<` – underloaded_operator Jun 15 '23 at 10:09
  • the error: _"If I replace this line with int x = *(nodeA->next) it gives me an error saying - no suitable function conversion from node to int exists"_ is a syntax error. We use the `->` operator to access members through a pointer, and you are trying to assign an object of type `node` to an `int`, which can't work unless you define a method that does such a thing. If you want to print the data of the node: `int x = nodeB -> data` since the next node of `A` is `B` I simplified `nodeA -> next` to `nodeB` – underloaded_operator Jun 15 '23 at 10:13

1 Answers1

4

There are two issues in your code:

//  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:

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:

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96