0
#include <iostream>
#include <string>
using namespace std;

class Person{
private:
    string name;
    int age, height, weight;
public:
    Person(string name = "empty", int age = 0, int height = 0, int weight = 0) {
        this->name = name;
        this->age = age;
        this->height = height;
        this->weight = weight;
    }
};

class Node {
public:
    Person* data;
    Node* next;
    Node(Person*A) {
       data = A;
        next = nullptr;
    }
};

class LinkedList {
public:
    Node * head;
    LinkedList() {
        head = nullptr;
    }

    void InsertAtHead(Person*A) {
        Node* node = new Node(A);
        node->next = head;
        head = node;
    }

    void Print() {
        Node* temp = head;
        while (temp != nullptr) {
            cout << temp->data << " ";
            temp = temp->next;
        }
        cout << endl;
    }
};

int main() {
    LinkedList* list = new LinkedList();

    list->InsertAtHead(new Person("Bob", 22, 145, 70));    list->Print();
}

When I run the Print method my code will print the memory location that Person is being stored. I tried to run through the code with a debugger but I am still confused and I am new to C++ and only a college student. I am guessing this has something to do with my print class and the line specifically "cout << temp->data << " ";" but I am not 100% sure. Could someone please explain how to fix this and why it will work? Thanks in advance!

Fall0ut
  • 71
  • 1
  • 12
  • Please see [C++ print value of a pointer](https://stackoverflow.com/questions/2485565/c-print-value-of-a-pointer) – Nybbit Mar 03 '18 at 07:01

3 Answers3

2

Type of Node::data is Person*. It makes sense that

cout << temp->data << " ";

prints only a pointer.

If you want to print the object, you'll have to use:

cout << *(temp->data) << " ";

However, before you can use that, you'll have to define a function overload that supports that operation. Define a function with the following signature:

std::ostream& operator(std::ostream& out, Person const& person)
{
   // Print the details of person.

   // Return the same ostream object
   return out;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Okay first parts make sense to me but Im confused about the function overload. Why does this have to be done? Is there another link that can reference me on how to do this better? Thanks! – Fall0ut Mar 03 '18 at 07:13
  • 1
    @Fall0ut, Browse the answers at https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading. Hopefully it will help. – R Sahu Mar 03 '18 at 07:15
0

In order to print the value of a pointer, you need to dereference it with *.

So, you'll want to use std::cout << *(temp->data); in order to get the value of data which is a Person*.

More here about dereferencing pointers.

Nybbit
  • 293
  • 2
  • 10
0

temp->data with the cout resolves to a Person pointer. To fix this you could call a method on the Person pointer (i.e. the object) that would return a string

solstice333
  • 3,399
  • 1
  • 31
  • 28