使用多个类和链接列表

Using multiple classes and linked lists

本文关键字:链接 列表      更新时间:2023-10-16

当前我正在尝试使用多个类(每个类别都有自己的.cpp和header .h文件),并使用main .cpp链接它们。我想制作一个临时的新视频对象指针,传递参数,将其插入链接列表,然后删除临时指针。之后,我需要打印列表的每个节点。

当前有4个文件:main.cpp,vlist.cpp,vlist.h,video.cpp和video.h

我正在使用VLIST作为构造链接列表的一种方式,该列表将通过VLIST.CPP文件中定义的插入函数在视频对象指针中传递。第一个问题是我不确定我是否正确地执行此操作。目前,我所做的一切都可以通过其他类中的视频对象进行。vlist.h文件。

第二个问题是我无法弄清楚如何正确访问每个节点中的单个视频对象属性,因为我的getter函数(在video.h中定义)无法正常工作。他们似乎返回地址而不是值。但是,每当我尝试解决这个问题时,它都会告诉我我不能像这样使用getter函数。

我的第三个也是最后一个问题是,在vlist.cpp中,我在创建一个新节点时不能传递m_vid,但是我可以通过m_head传递。,如果我不这样做,它不会编译使用myvid(vlist.h中的公开声明为视频对象指针。

下面的文件:

main.cpp

#include <iostream>
using namespace std;
#include "vlist.h"
#include "video.h"
int main()
{
    //Create temporary video object pointer using Video * temp = new Video(arguments);
    //Pass in the temp video pointer to the list and insert it with VList function
    string firstLine, secondLine, thirdLine = "";
    float fourthLine = 1.1;
    int fifthLine = 2;
    
    VList list;
    
    Video * tempVid = new Video(firstLine, secondLine, thirdLine, fourthLine, fifthLine);
    list.insert(tempVid);
    delete tempVid;
    list.print();
    return 0;
}

video.cpp

#include "video.h"
#include <iostream>
using namespace std;
Video::Video(string title, string URL, string comment, float length, int rating) {
    vidTitle = title;
    vidURL = URL;
    vidComment = comment;
    vidLength = length;
    vidRating = rating;
}
void Video::print(Video *myVid) {
    cout << myVid->getTitle() << endl;
}

video.h

#ifndef VIDEO_H
#define VIDEO_H
#include <string>
#include <iostream>
using namespace std;
class Video
{
    public:
        Video(string title, string URL, string comment, float length, int rating);
        int getRating() {
            return vidRating;
        }
        float getLength() {
            return vidLength;
        }
        string getTitle() {
            return vidTitle;
        }
        string getURL() {
            return vidURL;
        }
        string getComment() {
            return vidComment;
        }
        void print(Video *myVid);
    private:
        string vidTitle, vidURL, vidComment, vidPreference;
        float vidLength;
        int vidRating;
};
#endif

vlist.cpp

#include <iostream>
using namespace std;
#include "vlist.h"

VList::VList() {
    m_head = NULL;
}
VList::~VList() {
    Node *ptr = m_head;
    while (ptr != NULL) {
        Node *temp;
    
        temp = ptr;
        ptr = ptr->m_next;
        delete temp;
    }
}
void VList::insert(Video *myVid) {
    m_head = new Node(myVid, m_head);
}
void VList::print() {
    Node *ptr = m_head; 
    while (ptr != NULL) {
        cout << ptr->m_vid->getTitle();
        ptr = ptr->m_next;
    }
}

vlist.h

#ifndef VLIST_H
#define VLIST_H
#include "video.h"
class VList
{
    public:
        VList();
        ~VList();
        void insert(Video *myVid);
        void print();
        Video *myVid;
        
    private:
        class Node
        {
            public:
                Node(Video *myVid, Node *next) {    
                    m_vid = myVid; 
                    m_next = next;
                }
                Video *m_vid;
                Node *m_next;
        };
        Node *m_head;   
};
#endif

第一个问题是我不确定自己做得正确。目前,我所做的一切都可以通过另一个类是在vlist.h文件中包括video.h。

no ,您无法正确执行此操作,在文件main.cpp中,您正在创建一个指针到Video(即Video*)并将其传递给void VList::insert(Video *myVid)函数,在下一行中,您将删除您打印之前的指针。请记住,当您创建指针并将其传递到一种方法时,其生命周期不会像魔术一样自动管理时,您自己需要管理指针(这也是初学者面临的最常见问题,我也是如此)。因此,这个问题有两个解决方案

第一个修复

不删除main中的指针,因为它已在VList的破坏者中删除。

#include <iostream>
using namespace std;
#include "vlist.h"
#include "video.h"
int main()
{
    //Create temporary video object pointer using Video * temp = new Video(arguments);
    //Pass in the temp video pointer to the list and insert it with VList function
    string firstLine, secondLine, thirdLine = "";
    float fourthLine = 1.1;
    int fifthLine = 2;
    VList list;
    Video * tempVid = new Video(firstLine, secondLine, thirdLine, fourthLine, fifthLine);
    list.insert(tempVid);
    // delete tempVid; // don't delete this pointer right here, since I've found that you are deleting the pointer in the destructor of VList
    list.print();
    return 0;
}

第二修复

您可能想使用C 11的智能指针,这些都是标准化的!参见std::unique_ptrstd::shared_ptr。他们将自动删除指针并保证不会泄漏记忆。

第二个问题是我无法弄清楚如何正确访问每个节点中的单个视频对象属性,因为我的getter函数(在视频中定义)不起作用。

您的第二个问题与第一个问题有关,因为您在使用指针之前会删除指针,这会导致不确定的行为,并且您可能获得的输出就像垃圾。不是吗?

为了简单起见,我建议使用简单的Video参考而不是指针。按价值传递,您的所有问题都会蒸发。

回答我自己的问题以及可能看到这个问题的问题。我只需要对其进行一些更改,并在打印内部设置一个temp对象指针,然后将get函数插入其中。已经很晚了,所以如果有任何错误,我深表歉意。我确实得到了一个我想的地址。

void VList::print() {
    Node *ptr = m_head; 
    while (ptr != NULL) {
        Video *tempPtr = ptr->m_vid;
        cout << tempPtr->getTitle() << endl;
        ptr = ptr->m_next;
    }
}