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

How do you properly call a print function from another class using linked list?

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

Note: I am not allowed to use any form of input or output in my Vlist class. However, I can call Video's print function.

当我尝试在 void Vlist::print() 中调用 video.cpp 的打印函数时出现问题(或者我认为如此)。我一直收到分段错误。

这是我的代码:

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, string video_link, string video_comment, double video_length, 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;
     }

 }

Also note that I am aware that some of my code in main.cpp and vlist.cpp are incomplete, I am merely trying to work in parts. Which is currently receiving an output back.

同样,我不完全确定为什么会出现分段错误。每当我完成输入时,该程序只会给我一个段错误。 我对链表甚至指针仍然很陌生,因此,我们将不胜感激

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

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

main(){

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