2013-03-12 61 views
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; 
    }; 

的main.cpp

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 

    } 

video.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

当您编写“这是错误来自哪里”时,任何人都不明白该错误是什么。添加错误描述会很有礼貌。 – molbdnilo 2013-03-12 10:31:22

回答

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); 
    } 
    // ... 
} 
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; 
相关问题