如何使用链表从另一个类正确调用打印函数?

问题描述

我有一个使用5个参数输入的现有程序。输入的是视频标题,URL,评论,长度和等级。全部包含在video.cppvideo.h中。 然后,根据视频标题,按已排序的顺序将这些输入插入到链接列表vlist.cppvlist.h中。排序完成后,我需要能够打印新的排序列表。

注意:我的 Vlist类中不允许使用任何形式的输入或输出。但是我可以打电话 视频的打印功能

当我尝试在video.cpp调用void Vlist::print()的打印功能时(或我认为),会发生我的问题。我不断收到细分错误

这是我的代码

main.cpp

#include <iostream>
#include <stdlib.h>
#include <cstring>
using namespace std;

#include "video.h"
#include "vlist.h"


int main()
{
  string sort_type,url,comment,title;
    int rating;
    double length;
    int initial = 0,last = 0;

    Video *videoObj; 
    Vlist *vlistObj;

  while (getline(cin,title)) {

        getline(cin,url);
        getline(cin,comment);
        cin >> length;
        cin >> rating;
        cin.ignore();


        vlistObj->Insert(videoObj);
        initiaL++;
        last++;
    }

        vlistObj -> print();

return 0;
}

video.cpp

#include <iostream>
#include <algorithm>
using namespace std;

#include "video.h"
#include "vlist.h"

Video::Video(string video_title,string video_link,string video_comment,double video_length,int video_number)
  : title(video_title),link(video_link),comment(video_comment),length(video_length),rating(video_number)
{
  name = title;
}


bool Video::Title(Video *spare3)
{
  return name > spare3-> name;

}
void Video::print(){

  string hierarchy;
  switch(rating){

    case 1:
      hierarchy = "*";
      break;
    case 2:
      hierarchy = "**";
      break;
    case 3:
      hierarchy = "***";
      break;
    case 4:
      hierarchy = "****";
      break;
    case 5:
      hierarchy = "*****";
      break;

  }

  cout << title << "," << link << "," << comment << "," << length << "," << hierarchy << endl;

}

video.h

#ifndef VIDEO_H
#define VIDEO_H

#include "vlist.h"

class Vlist;

class Video {

  public:
    Video(string video_title,int video_number);
    void print();
    bool rating(Video *spare);
    bool Length(Video *spare2);
    bool Title(Video *spare3);
    const string& GetTitle() const { return title; }




  private:

    std::string title;
    string name;

    string link;
    string comment;
    double length;
    int rating;

};


#endif

vlist.h

#ifndef VLIST_H
#define VLIST_H

#include "video.h"



// forward declaration of class Video
 class Video;

class Vlist {
 public:

 Vlist() {m_head = nullptr; }
 void Insert(Video *video);
  void print();
 private:
 class Node {
        public:
                Node(Video *video,Node *next) {m_video = video; m_next = next; }
                Video *m_video;
                Node *m_next;
            };
            Node *m_head;
 };




#endif

vlist.cpp

#include <iostream>
#include <algorithm>
using namespace std;

#include "vlist.h"
#include "video.h"




void Vlist::Insert(Video* video)
{
    if (m_head == NULL || m_head->m_video -> GetTitle() > video->GetTitle())
    {
        // Insert before head...
        m_head = new Node(video,m_head);

    }
    else
    {
        // Insert after existing node...
        Node *node = m_head;
        while (node->m_next != NULL && node->m_next -> m_video->GetTitle() < video->GetTitle())
        {
            
            node = node->m_next;
        }
        node->m_next = new Node(video,node->m_next);

    }

 }

 void Vlist::print()
 {
     Video *video;
     Node *node = m_head;

     while(node != NULL)
     {
         video->print();
         node = node->m_next;
     }

 }

还请注意,我知道 main.cpp vlist.cpp 中的某些代码是不完整的,我只是在尝试部分工作。当前正在接收输出

同样,我也不完全确定为什么会遇到细分错误。只要我完成输入,该程序只会给我一个错误。 我对链表甚至指针还很陌生,因此,我们将不胜感激

解决方法

在主函数中,您必须分配指针值。

  main(){
 Video *videoObj; 
    Vlist *vlistObj;
} // error

main(){

    Vlist *vlistObj = new vlistObj;
    while(...){
       ...
       Video *videoObj = new videoObj(title,url,...);
       vlistObj->Insert(videoObj);
       ...
    }
}