【发布时间】:2018-03-03 06:57:05
【问题描述】:
#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();
}
当我运行 Print 方法时,我的代码将打印 Person 正在存储的内存位置。我尝试使用调试器运行代码,但我仍然感到困惑,而且我是 C++ 新手,而且只是一名大学生。我猜这与我的打印类有关,特别是“cout data
【问题讨论】:
标签: c++ object linked-list