使用多个类别和链接列表

时间:2019-02-24 05:16:17

标签: c++ class linked-list

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

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

我正在使用vlist作为一种构造链表的方法,该链表使用vlist.cpp文件中定义的插入函数传递到视频对象指针中。 第一个问题是我不确定我是否正确地完成了操作。此刻,我要在另一个类中传递视频对象所做的全部工作就是将video.h包含在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(arguements);
    //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

2 个答案:

答案 0 :(得分:2)

  

第一个问题是我不确定自己是否正确执行了操作。   目前,我正在做的就是能够在其中传递视频对象   另一个类是通过在vlist.h文件中包含video.h。

,您的操作不正确,您正在文件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(arguements);
    //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.h中定义)无效。

您的第二个问题与第一个问题有关,因为您在使用指针之前先删除了它,这会导致未定义的行为,您可能得到的输出就像垃圾一样。是吗?

为了简单起见,我建议使用简单的Video引用而不是指针。通过价值传递它们,您所有的问题都会消失。

答案 1 :(得分:0)

回答我自己的问题以及可能看到此问题的任何人的问题。我只需要对其稍作更改,并在print中设置一个临时对象指针,然后在其上指定一个get函数即可。已经很晚了,所以我很抱歉如果有任何错误。 我确实得到了我想像的地址。

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