Kerita 2014-06-15 09:02 采纳率: 0%
浏览 868

不知道为什么output()无法接收information这个对象,求大神指点

前面定义了两个类,Node类是LinkedList类的数据成员,主函数中不知道为什么output()无法接收information这个对象,求大神指点

#include
#include
#include
using namespace std;
#include"string"

class Node
{

private:

Node * next;

public:

friend class LinkedList;

long id;

string name;

string sex;

string workplace;

string telephone;

string mail;

Node(long id=0, string name="nothing", string sex="nothing", string workplace="nothing", 
    string telephone="nothing", string mail="nothing",Node *next=NULL);

void insertAfter(Node *p);

Node *deleteAfter();

Node *nextNode();

void SetNext(Node *ptr)
{
   next=ptr;
}

};

Node::Node(long id, string name, string sex, string workplace, string telephone, string mail,Node *next):
id(id),name(name),sex(sex),workplace(workplace),telephone(telephone),mail(mail),next(next){}

Node * Node::nextNode()
{
return next;
}

void Node::insertAfter(Node *p)
{
p->next = next;

next = p;

}

Node * Node::deleteAfter()
{
//定义tempPtr使函数可以返回删除的节点地址
Node * tempPtr = next;

if(next == 0)

    return 0;

next = tempPtr->next;

return tempPtr;

}

class LinkedList {

private:

    //表头和表尾指针
    Node *front, *rear;  

    //记录表当前遍历位置的指针,由插入和删除操作更新
    Node *prevPtr, *currPtr; 

    //表中元素个数
    int size;

    //当前元素在表中的位置序号,由函数reset()使用
    int position;


   //释放节点
    void freeNode(Node *p);

   //将链表L复制到当前表(假设当前为空)
   //被复制构造函数和operator=函数调用
    void copy(const LinkedList &L);


public:

    void print()
    {
        if(size=0||!currPtr)
        {
            cout<<"通讯录为空。"<<endl;

            exit(0);
        }

    else
    {
       cout<<setw(5);
       cout<<"学号"<<"  ";

        cout<<setw(7);
        cout<<"姓名"<<"  ";

         cout<<setw(7);
        cout<<"性别"<<" ";


       cout<<setw(10);
        cout<<"工作地点"<<"  ";

        cout<<setw(12);
        cout<<"电话";

         cout<<setw(15);
        cout<<"邮箱"<<endl;


        while(currPtr!=NULL)

        {

        cout<<setw(5);
       cout<<currPtr->id<<"  ";

        cout<<setw(7);
        cout<<currPtr->name<<"  ";

         cout<<setw(7);
        cout<<currPtr->sex<<" ";

        cout<<setw(10);
        cout<<currPtr->workplace<<"  ";

       cout<<setw(12);
        cout<<currPtr->telephone<<"  ";

        cout<<setw(15);
        cout<<currPtr->mail<<endl;


        currPtr = currPtr->next;
        }

    }


}


    LinkedList();


    LinkedList(const LinkedList &L);


    ~LinkedList();


    LinkedList &operator =(const LinkedList &L);


   //生成新节点,数据域为item,指针域为ptrNext
    Node *newNode(long id=0, string name="nothing", string sex="nothing", string workplace="nothing", 
    string telephone="nothing", string mail="nothing", Node *ptrNext=NULL);

    //返回链表中元素的个数
    int getSize()const;

    //检查链表是否为空
    bool isEmpty()const;

    //初始化游标位置
    void reset(int pos=0);

    //使游标移动到下一个节点
    void next();

    //游标是否到链尾
    bool endOfList()const;

    //返回游标当前位置
    int currentPosition(void) const;



    //在表头插入节点
    void insertFront(Node *p);

    //在表尾添加节点
    void insertRear(Node *p);

    //在当前节点之前添加节点
    void insertAt(Node *p);

    //在当前节点之后添加节点
    void insertAfter(Node *p);




    //删除头结点
     void deleteFront();

    //删除当前节点
    void deleteCurrent();



    //清空链表:释放所有节点的内存空间。被析构函数和operator=调用 
    void clear();

};

//LinkedList.cpp

LinkedList::LinkedList():front(NULL),rear(NULL),prevPtr(NULL),currPtr(NULL),size(0),position(-1){};

LinkedList::LinkedList(const LinkedList &L){
copy(L);
}

LinkedList::~LinkedList(){
clear();
}

LinkedList & LinkedList::operator = (const LinkedList &L)
{
if(this!=&L)//与原来的比较
{
clear();

    copy(L);
}   

return *this;

}

int LinkedList::getSize()const
{
return size;
}

bool LinkedList::isEmpty()const
{
if(size==0)
return false;
else
return true;
}

void LinkedList::freeNode(Node *p)
{
delete p;
}

void LinkedList::copy(const LinkedList &L)
{
Node *ptr = L.front;

currPtr=NULL;

while(ptr)  //遍历插入
{
    insertAfter(ptr);  

    ptr=ptr->nextNode();
}

if(position==-1)//空链表 
    return;

prevPtr=NULL;

currPtr=front;

for(int pos=0;pos!=L.currentPosition();pos++) //遍历到结尾
{
    prevPtr=currPtr;
    currPtr=currPtr->nextNode();
}

position=L.position;

size=L.size;

}

//pos默认值为0
void LinkedList::reset(int pos)
{
int n;
//链表为空,直接退出
if(!front)
return;

if(pos>=size||pos<0)  //位置规范
{
    cout<<"reset:position error"<<endl;
    return; 
}

if(pos==0)   //默认将指针位置放在开头
{
    prevPtr=NULL;
    currPtr=front;
    position=0;
}

else//其他位置
{
    prevPtr=front;
    n=1;
    for(position=n;position!=pos;position++)//指针移动到pos位置
        {
            prevPtr=currPtr;
            currPtr=currPtr->nextNode();
        }

}

}

void LinkedList::next()
{
if(currPtr)//判断是否为空链表
{
prevPtr=currPtr;
currPtr=currPtr->nextNode();
position++;
}
}

bool LinkedList::endOfList()const
{
return (!currPtr);
}

int LinkedList::currentPosition(void) const
{
return position;
}

void LinkedList::insertFront(Node *p)
{

if(front==NULL)

{

 front=rear=p;

 size++;

}

else

{
p->next=front;

front=p;

size++;

}

FILE *fp;
fp= fopen("通讯录.txt","w");
fwrite(p,sizeof(Node),1,fp);
fclose(fp);

}

void LinkedList::insertRear(Node *p)
{
//链表为空
if(front==NULL)

 {
  front=rear=p;

  size++;

 }

else

{

 rear->next=p;

rear=p;

size++;

}

  FILE *fp;
  fp=fopen("通讯录.txt","a");
  fwrite(p,sizeof(Node),1,fp); 
  fclose(fp);

}

void LinkedList::insertAt(Node *p)

{

  if(currPtr==front)

  insertFront(p); //在当前结点之前插入结点

  else
  {
      p->next=currPtr;

      prevPtr->next=p;

      size++;
  }


  FILE *fp;

  fp= fopen("通讯录.txt","w");

  fseek(fp, 0L, 0);

  fwrite(p, sizeof(Node), 1,fp);

}

void LinkedList::insertAfter(Node *p)
{
if(currPtr==rear)

{

   insertRear(p);

}

else

{
    p->next=currPtr->next;

    currPtr->next=p;

    size++;
}

}

void LinkedList::deleteFront()
{
Node *ptr=front;
if(front) //判断是否为空链表
{
front=front->nextNode();
delete ptr;
size--;
}
else
{

cout<<"This list is empty!"<<endl;
}
}

void LinkedList::deleteCurrent()
{
Node *ptr;

if(!currPtr)      //空链表 
{
    cout<<"This list is a empty!"<<endl;
    exit(1);
}
if(!prevPtr)     //删除头结点 
{   
    ptr=front;
    front=front->nextNode();
} 

ptr=prevPtr->deleteAfter();


if(ptr==rear)    //若删除的为表尾
{
    rear=prevPtr;
    position--;
}

currPtr = ptr->nextNode();
freeNode(ptr); 
size--;

}

void LinkedList::clear()
{
Node *ptr1,*ptr2;

ptr1=front;

while(ptr1) //遍历删除
{
    ptr2=ptr1->nextNode();

    delete ptr1;

    ptr1=ptr2;
}

front=NULL;
rear=NULL;
prevPtr=NULL;
currPtr=NULL;
size=0;
position=-1;

}

int main()
{

long id;

string name;

string sex;

string workplace;

string telephone;

string mail;



cout<<endl<<"           \3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3"<<endl;

cout<<endl;

cout<<"                         欢迎进入学生信息管理系统——春荣"<<endl;

cout<<endl;

cout<<"           \3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3"<<endl;

cout<<endl;


LinkedList information;
Node *p =new Node(3,"春荣","男","华师","1234334424","123113123137@qq.com");
information.insertRear(p);

p = new Node(2,"倩倩","女","华师","63716437823","1673646317@163.com");
information.insertFront(p);

void output(LinkedList list);
void change(LinkedList list);
void addup(LinkedList list);
void search(LinkedList list);

int choice;

while(1)
{

cout<<"请选择您需要的功能:"<<endl<<endl;
cout<<"1——查看所有人信息"<<endl<<endl;
cout<<"2——修改姓名跟电话号码"<<endl<<endl;
cout<<"3——统计功能"<<endl<<endl;
cout<<"4——由学号查询个人信息"<<endl<<endl;;
cout<<"5——退出"<<endl<<endl;

cout<<"请输入:";
cin>>choice;



switch(choice)
{
case 1:
    cout<<"11"<<endl;
    output(information); //出现错误的地方
    break;

case 2:
    change(information);
    break;

case 3:
   addup(information);
   break;

case 4:
    search(information);
    break;

case 5:
    break;

default:
    cout<<"您的输入有误,重新输入。"<<endl<<endl;
    break;

}

if(5 == choice)
    break;


}

return 0;
}

void output(LinkedList list)
{

    cout<<"1"<<endl;

    list.print();   

}

void change(LinkedList list)
{

cout<<"Do you really want to change"<<endl;

}

void addup(LinkedList list)
{
cout<<"综合"<<endl;

}

void search(LinkedList list)
{
cout<<"寻找"<<endl;
}

  • 写回答

1条回答

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2023-03-16 12:39
    关注

    该回答引用ChatGPT

    从代码来看,问题可能是在调用 output() 函数时,没有传入一个 information 类型的对象作为参数。


    假设你的 information 类是这样定义的:

    class information {
    public:
        long id;
        string name;
        string sex;
        string workplace;
        string telephone;
        string mail;
    };
    

    那么,如果你想在 LinkedList 类中的 print() 函数中使用 output() 函数,你需要将链表中每个节点的信息转换成 information 类型的对象,然后再传递给 output() 函数。


    以下是可能的解决方案:

    void LinkedList::print() {
        // 遍历链表
        Node *p = front;
        while (p) {
            // 将节点信息转换为 information 类型的对象
            information info;
            info.id = p->id;
            info.name = p->name;
            info.sex = p->sex;
            info.workplace = p->workplace;
            info.telephone = p->telephone;
            info.mail = p->mail;
    
            // 调用 output() 函数
            output(info);
    
            // 移动到下一个节点
            p = p->next;
        }
    }
    

    这个解决方案假设 output() 函数已经被正确定义,并且接受一个 information 类型的对象作为参数。

    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误