2013-03-12 65 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; 
相關問題