鱼C论坛

 找回密码
 立即注册
查看: 2462|回复: 4

链表的数据域是一个类对象

[复制链接]
发表于 2019-7-8 21:25:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
语法没有错误,但是控制台显示分配不了内存,不知道是哪的逻辑错误?求大佬解决下

#include "stdafx.h"
#include "iostream"
using namespace std;

class person
{
private:
        string name;
        int age;
        char sex;
public:
        void getname();
        void getage();
        void getsex();
        void putname();
        void putage();
        void putsex();
};

void person::getname()
{
        cin >> this->name;
}

void person::getage()
{
        cin >> this->age;
}

void person::getsex()
{
        cin >> this->sex;
}

void person::putname()
{
        cout << "姓名:" << name <<endl;
}

void person::putage()
{
        cout<<"年龄:" << age << endl;
}

void person::putsex()
{
        cout << "性别" << sex << endl;
}

struct node
{
        person * A;
        struct node * pnext;
};

struct node * creat_list(void)
{
        //创建一个头指针
        struct node * phead;
        struct node * pnext;
        struct node * pnew;

        pnext = phead;
       
        pnext->A = new person;
        pnext->A->getname();
        pnext->A->getage();
        pnext->A->getsex();
        pnext->pnext = NULL;
       
        int num;
        cin >> num;
        for (int i=0;i<num;i++)
        {
        pnew->A = new person;
       
        pnew->A->getname();
        pnew->A->getage();
        pnew->A->getsex();

        pnew->pnext = NULL;
        pnext->pnext = pnew;
        pnext = pnew;
        }
        return phead;

}

int main ()
{
        struct node * phead;
        phead = creat_list();
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-7-9 08:56:57 | 显示全部楼层
你的name 是string类型的,不能直接cin输入!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-9 09:15:54 | 显示全部楼层
E:\tmp>g++ -g -Wall -o main main.cpp
main.cpp: In function 'int main()':
main.cpp:91:23: warning: variable 'phead' set but not used [-Wunused-but-set-variable]
         struct node * phead;
                       ^~~~~
main.cpp: In function 'node* creat_list()':
main.cpp:63:15: warning: 'phead' is used uninitialized in this function [-Wuninitialized]
         pnext = phead;
         ~~~~~~^~~~~~~
main.cpp:75:17: warning: 'pnew' may be used uninitialized in this function [-Wmaybe-uninitialized]
         pnew->A = new person;
         ~~~~~~~~^~~~~~~~~~~~

E:\tmp>

如果编译器说你有可能错了,那么你很有可能就是错了
不要忽略编译器的警告
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-9 13:38:20 | 显示全部楼层
你定义的person类中使用了string类型的数据,而我们知道,string在使用时会自己分配内存,那么,在你使用指针的时候,如果要执行  pnext->A = new person; 在这里,你这个类的实例分配的内存大小怎么确定,推荐这里使用数组试试。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-7-10 21:36:36 From FishC Mobile | 显示全部楼层
人造人 发表于 2019-7-9 09:15
如果编译器说你有可能错了,那么你很有可能就是错了
不要忽略编译器的警告

这我也看不懂咋错了啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-3 23:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表