若水15 2021-12-16 22:28 采纳率: 0%
浏览 16

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

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

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

```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;
}

###### 直接崩掉了

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

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

```

  • 写回答

2条回答 默认 最新

  • CSDN专家-link 2021-12-16 22:33
    关注

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

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 12月16日

悬赏问题

  • ¥60 iinfercnv 肿瘤进化树
  • ¥15 如何入门学习c语言,单片机
  • ¥15 idea 编辑语言的选择
  • ¥15 Windows下部署Asmjit
  • ¥15 请问双层规划模型的上下层目标函数不一致,是如何保证迭代收敛性的
  • ¥15 微信小程序 前端页面内容搜索
  • ¥15 cpu是如何判断当前指令已经执行完毕,然后去执行下条指令的
  • ¥15 安装visual studio2022时visualstudiosetup启动不了,闪退。问题代号0x0和0x1389
  • ¥30 java spring boot2.5.3版本websocket连不上
  • ¥15 angular js调外部链接查看pdf