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

C++ 如何使用链表从另一个类正确调用打印函数?,c++,class,pointers,linked-list,function-call,C++,Class,Pointers,Linked List,Function Call,我有一个现有的程序,它接受5个参数的输入。输入内容包括视频标题、url、评论、长度和分级。全部包含在video.cpp和video.h中。 然后根据视频标题,将这些输入按排序顺序插入链表,vlist.cpp和vlist.h。排序完成后,我需要能够打印新的排序列表 注意:我不允许在我的Vlist类中使用任何形式的输入或输出。不过,我可以打电话 视频的打印功能 当我试图在void Vlist::print()中调用video.cpp的print函数时,我的问题就出现了(我想大概是这样)。我不断得到一

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

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

当我试图在
void Vlist::print()
中调用
video.cpp
的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;
}
#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;

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

 }
vlist.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
#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 <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;
}
#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;

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

 }
#包括
#包括
使用名称空间std;
#包括“vlist.h”
#包括“video.h”
void Vlist::插入(视频*视频)
{
如果(m|u head==NULL | m|u head->m|u video->GetTitle()>video->GetTitle())
{
//在头之前插入。。。
m_头=新节点(视频,m_头);
}
其他的
{
//在现有节点后插入。。。
节点*节点=m_头;
while(node->m_next!=NULL&&node->m_next->m_video->GetTitle()GetTitle())
{
node=node->m_next;
}
node->m_next=新节点(视频,node->m_next);
}
}
void Vlist::print()
{
视频*视频;
节点*节点=m_头;
while(节点!=NULL)
{
视频->打印();
node=node->m_next;
}
}
还要注意的是,我知道我在main.cppvlist.cpp中的一些代码是不完整的,我只是在尝试部分工作。当前正在接收返回的输出

同样,我也不完全确定为什么我会出现分割错误。每当我完成输入时,程序只会给我一个seg故障。
我对链表甚至指针还是很陌生,因此,如果有任何帮助,我将不胜感激。

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

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

main(){

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

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

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

main(){

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

您是否尝试过在调试器中逐行运行代码,同时监视所有变量的值,以确定程序在哪一点停止按预期运行?如果您没有尝试此方法,那么您可能想阅读以下内容:您可能还想阅读此内容:。@AndreasWenzel我确实使用了
gbd a.out
,但老实说,我仍然对手头的问题感到非常困惑。正如前面提到的,问题出在
video.cpp
中,但是我已经测试了
video.cpp
,并收到了正确的输出(在输入到链接列表之前)。该错误仅在我输入
void Vlist::print()
时发生。但是我会进一步研究你提供的链接如果你已经知道gdb,那么我认为你不需要阅读我发布的链接。不过,这可能仍然是一本有趣的读物。您可以通过删除大部分分配的需求来简化示例代码。不要从用户处读取值;硬编码要使用的值(或者只从用户那里获取值,然后删除所有内容)。视频的标题可能很重要,因为它是排序键,但从
视频中删除所有其他数据。继续删除不需要的内容,直到错误无处隐藏。此外:并在发布之前解决这些警告。您是否尝试过在调试器中逐行运行代码,同时监视所有变量的值,以确定程序在哪一点停止按预期运行?如果您没有尝试此方法,那么您可能想阅读以下内容:您可能还想阅读此内容:。@AndreasWenzel我确实使用了
gbd a.out
,但老实说,我仍然对手头的问题感到非常困惑。正如前面提到的,问题出在
video.cpp
中,但是我已经测试了
video.cpp
,并收到了正确的输出(在输入到链接列表之前)。该错误仅在我输入
void Vlist::print()
时发生。但是我会进一步研究你提供的链接如果你已经知道gdb,那么我认为你不需要阅读我发布的链接。不过,这可能仍然是一本有趣的读物。您可以通过删除大部分分配的需求来简化示例代码。不要从用户处读取值;硬编码要使用的值(或者只从用户那里获取值,然后删除所有内容)。视频的标题可能很重要,因为它是排序键,但从
视频中删除所有其他数据。继续删除不需要的内容,直到错误无处隐藏。另外:在发布您的之前,请先解决这些警告。因此,当我尝试这样做时,它会显示错误:“videoObj”之前需要类型说明符,而vlistObjI也需要类型说明符,我刚刚修复了。再试一次。它仍然给我同样的错误。编程是如此混乱,hahaokay,我替换了
Vlist*vlistObj=新的vlistObj带有
Vlist*vlistObj=新的Vlist似乎正在工作。谢谢,所以当我尝试这样做时,它会显示错误:“videoObj”之前应该有类型说明符,vlistObjI也应该有类型说明符,我刚刚修复了。再试一次。它仍然给我同样的错误。编程是如此混乱,hahaokay,我替换了
Vlist*vlistObj=新的vlistObj带有
Vlist*vlistObj=新的Vlist似乎正在工作。非常感谢。