为啥链表里面的数据不能调用函数

写代码时发现当数据域时结构体或类对象时,不能调用该结构体或类对象的成员函数,这是为啥呀

问题相关代码,请勿粘贴截图

```c++
#include<iostream>
#include<string>
#include<malloc.h>
using namespace std;
class Person{
public:
    string m_Name;
    string m_Address;
    string m_Mobile;
    int m_cla;
    string m_QQ;
    Person operator=(Person a){
        this->m_Address=a.m_Address;
        this->m_cla=a.m_cla;
        this->m_Mobile=a.m_Mobile;
        this->m_Name=a.m_Name;
        this->m_QQ=a.m_QQ;
        return *this;
    }
    void PrintPerson(){
        cout<<"姓名: "<<this->m_Name<<"\t"
        <<"联系电话: "<<this->m_Mobile<<"\t"
        <<"家庭住址: "<<this->m_Address<<"\t"
        <<"QQ: "<<this->m_QQ;
        switch(this->m_cla){
        case 1:
            cout<<"类别: 家人"<<endl;
            break;
        case 2:
            cout<<"类别: 朋友"<<endl;
            break;    
        case 3:
            cout<<"类别: 同事"<<endl;
            break;
        }
    }
    void Add(){
        cout<<"请输入所添加联系人的";
        cout<<"1、姓名: ";
        cin>>this->m_Name;
        cout<<"2、联系电话: ";
        cin>>this->m_Mobile;
        cout<<"3、家庭住址: ";
        cin>>this->m_Address;
        cout<<"4、姓名: ";
        cout<<"1、家人 "<<endl<<"2、朋友 "<<endl<<"3、同事"<<endl<<"请输入选择: ";
        cin>>this->m_cla;
        cout<<"5、QQ号: ";
        cin>>this->m_QQ;
        cout<<"添加完成!"<<endl;
        PrintPerson();
        system("pause");
        system("cls");
    }
};
struct Node{
    Person person;
    Node *next;
};
class Manager{
public:
    Manager(){
        head=(Node*)malloc(sizeof(Node));
        head->next=NULL; 
        end=(Node*)malloc(sizeof(Node));
        end->next=NULL; 
        this->PersonNum=0;
    }
    void AddPerson(){
        int num;
        cout<<"输入你想添加的人数: "; 
        cin>>num;
        this->PersonNum+=num;
        while(num--){
            Person human;
            human.Add();
            Node *newNode=(Node*)malloc(sizeof(Node));
            
            cout<<"zheli "<<endl;
            if(head->next=NULL){
                head->next=newNode;
                newNode->next=NULL;
                end=newNode;
                continue;
            }
            end->next=newNode;
            newNode->next=NULL;
            end=newNode;
        }
    }
    void PrintTotalPerson(){
        cout<<"通讯录当前人员个数为 "<<this->PersonNum<<" 人"<<endl; 
        cout<<"以下就是所有的已添加人员信息: "<<endl;
        Node *p=head->next;
        while(p){
            p->person.PrintPerson();
            p=p->next;
        }
        system("pause");
        system("cls");
    }
    ~Manager(){
        Node *p=head->next; 
        Node *w=head; 
        while(p){
            w=p;
            p=p->next;
            free(w);
            w=NULL;
            w->next=NULL;
        }
    }
private:
    Node *head;
    Node *end;
    int PersonNum;
};
int main(){
    Manager man;
    Person human;
    human.Add();
    Node *newNode=(Node *)malloc(sizeof(Node)+sizeof(Person));
    //就是这里,不能调用成员函数
    newNode->person=human;
    newNode->person.PrintPerson();
    //////////////////////////////////////
    return 0;
}

###### 直接崩掉了

###### 我的解答思路和尝试过的方法 

###### 可以使用运算符重载来快速的赋值和调用成员函数来方便打印

```

Node * newNode=(Node * )malloc(sizeof(Node)+sizeof(Person));
这里申请的空间为是么是sizeof(Node)+sizeof(Person)呢?
既然是C++了,就不要用malloc了,直接Node *newNode = new Node;就好了

类在new的时候才会给成员变量分配内存空间,所以,在newNode->person = human;这里的时候,person是没有被分配内存空间的。
所以运行到这个地方的时候会崩溃。
其次,Node *newNode=(Node *)malloc(sizeof(Node)+sizeof(Person));这里申请内存空间的时候,空间大小有问题,可能引发未知的错误。应该是Node *newNode=(Node *)malloc(sizeof(Node));


修改方法:
struct Node{
Person person;
Node *next;
};
这里把Person person; 修改为 Person *person;
main函数中
newNode->person = human;改成 newNode->person = &human;
newNode->person.PrintPerson(); 改成 newNode->person->PrintPerson(); (Manager类中也有一处)

完整代码如下:


#include<iostream>
#include<string>
#include<malloc.h>
using namespace std;
class Person
{
public:
    
    string m_Name;
    string m_Address;
    string m_Mobile;
    int m_cla;
    string m_QQ;
    Person operator=(Person a)
    {
        this->m_Address=a.m_Address;
        this->m_cla=a.m_cla;
        this->m_Mobile=a.m_Mobile;
        this->m_Name=a.m_Name;
        this->m_QQ=a.m_QQ;
        return *this;
    }
    void PrintPerson()
    {
        cout<<"姓名: "<<this->m_Name<<"\t"
            <<"联系电话: "<<this->m_Mobile<<"\t"
            <<"家庭住址: "<<this->m_Address<<"\t"
            <<"QQ: "<<this->m_QQ;
        switch(this->m_cla){
        case 1:
            cout<<"类别: 家人"<<endl;
            break;
        case 2:
            cout<<"类别: 朋友"<<endl;
            break;    
        case 3:
            cout<<"类别: 同事"<<endl;
            break;
        }
    }
    void Add(){
        cout<<"请输入所添加联系人的";
        cout<<"1、姓名: ";
        cin>>this->m_Name;
        cout<<"2、联系电话: ";
        cin>>this->m_Mobile;
        cout<<"3、家庭住址: ";
        cin>>this->m_Address;
        cout<<"4、姓名: ";
        cout<<"1、家人 "<<endl<<"2、朋友 "<<endl<<"3、同事"<<endl<<"请输入选择: ";
        cin>>this->m_cla;
        cout<<"5、QQ号: ";
        cin>>this->m_QQ;
        cout<<"添加完成!"<<endl;
        PrintPerson();
        system("pause");
        system("cls");
    }
};
struct Node{
    Person *person;  //修改1
    Node *next;
};
class Manager{
public:
    Manager(){
        head=(Node*)malloc(sizeof(Node));
        head->next=NULL; 
        end=(Node*)malloc(sizeof(Node));
        end->next=NULL; 
        this->PersonNum=0;
    }
    void AddPerson(){
        int num;
        cout<<"输入你想添加的人数: "; 
        cin>>num;
        this->PersonNum+=num;
        while(num--){
            Person human;
            human.Add();
            Node *newNode=(Node*)malloc(sizeof(Node));
            cout<<"zheli "<<endl;
            if(head->next=NULL){
                head->next=newNode;
                newNode->next=NULL;
                end=newNode;
                continue;
            }
            end->next=newNode;
            newNode->next=NULL;
            end=newNode;
        }
    }
    void PrintTotalPerson(){
        cout<<"通讯录当前人员个数为 "<<this->PersonNum<<" 人"<<endl; 
        cout<<"以下就是所有的已添加人员信息: "<<endl;
        Node *p=head->next;
        while(p){
            p->person->PrintPerson();
            p=p->next;
        }
        system("pause");
        system("cls");
    }
    ~Manager(){
        Node *p=head->next; 
        Node *w=head; 
        while(p){
            w=p;
            p=p->next;
            free(w);
            w=NULL;
            w->next=NULL;
        }
    }
private:
    Node *head;
    Node *end;
    int PersonNum;
};
int main(){
    Manager man;
    Person human;
    human.Add();
    Node *newNode=(Node *)malloc(sizeof(Node));//+sizeof(Person)
    //就是这里,不能调用成员函数
    newNode->person = &human;
    newNode->person->PrintPerson();
    //////////////////////////////////////
    return 0;
}