使用多个类和链接列表

问题描述 投票:-1回答:2

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

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

我使用vlist作为构建链接列表的方法,该链接列表通过vlist.cpp文件中定义的insert函数传递到视频对象指针中。第一个问题是我不确定我是否正确地这样做了。目前,我正在做的所有能够在另一个类中传递视频对象的方法是将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
c++ class linked-list
2个回答
2
投票

第一个问题是我不确定我是否正确地这样做了。目前,我正在做的所有能够在另一个类中传递视频对象的方法是将video.h包含在vlist.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引用而不是指针。按价值传递它们,你所有的问题都会消失。


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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.