0

vlist.h

 class Vlist
    {
    public:
        Vlist();
        ~Vlist();
        void insert(string title, string URL, string comment, float length, int rating);
        bool remove();

    private:
        class Node
        {
        public:
            Node(class Video *Video, Node *next)
            {m_video = Video; m_next = next;}
            Video *m_video;
            Node *m_next;
        };
        Node* m_head;
    };

主文件

 int main()
    {
    ....blah blah.... (cin the values)

            Array[i] = new Video(title, URL, comment, length, rating);
            Vlist objVlist;
            objVlist.insert(title, URL, comment, length, rating);
    }

vlist.cpp

这就是错误的来源

(m_head = new Node(Video, NULL); 

...此函数的工作是将指向视频类对象的指针插入到列表中。

 void Vlist::insert(string title, string URL, string comment, float length, int rating)
    {
        Node *ptr = m_head;
        //list is empty
        if(m_head == NULL)
            m_head = new Node(Video, NULL);
        else
        {
            ptr = ptr->m_next;
            ptr = new Node(Video, NULL);
        }
        //sort the list every time this executes

    }

视频.h

这是我试图指向使用链表的类。

class Video
{
public:
    Video(string title, string URL, string comment, float length, int rating);
    ~Video();
    void print();
    bool longer(Video *Other);
    bool higher(Video *Other);
    bool alphabet(Video *Other);
private:
    string m_title;
    string m_url;
    string m_comment;
    float m_length;
    int m_rating;
};

第一次使用堆栈溢出,不太清楚会发生什么。

4

2 回答 2

2

尝试改变

m_head = new Node(Video, NULL);

m_head = new Node(new Video(title, URL, comment, length, rating), NULL);

还有这个:

else
{
    ptr = ptr->m_next;
    ptr = new Node(Video, NULL);
}

不是真正将新添加Node到列表头部的正确方法。需要类似的东西:

ptr = new Node(new Video(title, URL, comment, length, rating), NULL);
ptr->m_next = m_head;
m_head = ptr;
于 2013-03-12T10:32:57.170 回答
1

“视频”是一个类的名称。
您需要创建一个Video实例。

void Vlist::insert(string title, string URL, string comment, float length, int rating)
{
    Video* v = new Video(title, URL, comment, length, rating);
    Node *ptr = m_head;
    if(m_head == NULL)
        m_head = new Node(v, NULL);
    else
    {
        ptr = ptr->m_next;
        ptr = new Node(v, NULL);
    }
    // ...
 }
于 2013-03-12T10:34:48.807 回答