2013-09-29 156 views
1

我無法使用指向視頻對象的指針的成員函數。 我的代碼如下:鏈接列表功能遇到問題

vlist.h:

5 #ifndef VLIST_H 
6 #define VLIST_H 
7 
8 #include<iostream> 
9 using namespace std; 
10 
11 #include<string> 
12 #include"video.h" 
13 
14 class Vlist 
15 { 
16 public: 
17 Vlist(); 
18 // ~Vlist(); 
19 void insert(Video *vid); 
20 // void insert_alphabetical(Video *vid); 
21 void print(); 
22 // void length(); 
23 bool lookup(string title); 
24 bool remove(string title); 
25 private: 
26 class Node 
27 { 
28 public: 
29  Node(Video *vid, Node *next) 
30  {m_vid = vid; m_next = next;} 
31  Video *m_vid; 
32  Node *m_next; 
33 }; 
34 Node *m_head; 
35 }; 
36 
37 #endif 

vlist.cpp:

5 #include<iostream> 
    6 using namespace std; 
    7 
    8 #include<string> 
    9 #include"vlist.h" 
10 #include"video.h" 
11 
12 Vlist::Vlist() 
13 { 
14 m_head = NULL; 
15 } 
16 
17 void Vlist::insert(Video *vid) 
18 { 
19 m_head = new Node(vid, m_head); 
20 } 
21 
22 void Vlist::print() 
23 { 
24 Node *ptr = m_head; 
25 while(ptr != NULL) 
26 { 
27 cout << ptr->m_vid->print(); 
28 ptr = ptr->m_next; 
29 } 
30 } 

video.h:

5 #ifndef VIDEO_H 
6 #define VIDEO_H 
7 
8 #include<iostream> 
9 using namespace std; 
10 
11 #include<string> 
12 
13 class Video 
14 { 
15 public: 
16 Video(string title, string url, string comment,float length, int rating); 
17 ~Video(); 
18 void print(); 
19 
20 private: 
21 string m_title; 
22 string m_url; 
23 string m_comment; 
24 float m_length; 
25 int m_rating; 
26 }; 
27 
28 #endif 

Video.cpp:

5 #include<iostream> 
6 using namespace std; 
7 
8 #include<string> 
9 
10 #include "video.h" 
11 
12 using namespace std; 
13 
14 Video::Video(string title, string url, string comment, float length, int rating) 
15 { 
16 m_title = title; 
17 m_url = url; 
18 m_comment = comment; 
19 m_rating = rating; 
20 m_length = length; 
21 } 
22 
23 /* Video::~Video() 
24 { 
25 cout << "object is desructing" << endl; 
26 } */ 
27 
28 void Video::print() 
29 { 
30 cout << m_title << ", " << m_url << ", " << m_comment << ", " << m_length << ", "; 
31 for(int i = 0; i < m_rating; i++) 
32 { 
33 cout << "*"; 
34 } 
35 cout << endl; 
36 } 
37 

main.cpp中:

5 #include<iostream> 
6 using namespace std; 
7 
8 #include<string> 
9 #include"video.h" 
10 #include"vlist.h" 
11 
12 int main() 
13 { 
14 int num_vids = 0; 
15 Video *vids; 
16 Vlist vlist; 
17 
18 string title, url, comment; // for title, url, and comment 
19 float length; // for the length 
20 int rating; // for the rating 
21 
22 string choice; // what the user wants to do 
23 
24 cout << "What would you like to do?" << endl; 
25 cout << "insert" << endl; 
26 cout << "print" << endl; 
27 cout << "length" << endl; 
28 cout << "lookup" << endl; 
29 cout << "remove" << endl; 
30 
31 while(getline(cin, choice)) 
32 { 
33 if((choice == "insert")) 
34 { 
35 getline(cin, title); 
36 getline(cin, url); 
37 getline(cin, comment); 
38 cin >> length; 
39 cin >> rating; 
40 cin.ignore(); 
41 
42 vids = new Video(title, url, comment, length, rating); 
43 vlist.insert(vids); 
44 num_vids++; 
45 // cout << "you would like to insert something" << endl; 
46 } 
47 
48 else if((choice == "print")) 
49 { 
50 // cout << "you would like to print something" << endl; 
51 vlist.print(); 
52 } 
53 
54 else if((choice == "length")) 
55 { 
56 cout << num_vids << endl; 
57 } 
58 
59 else if((choice == "lookup")) 
60 { 
61 cout << "you would like to look something up!" << endl; 
62 } 
63 
64 else if((choice == "remove")) 
65 { 
66 cout << "you would like to remove something" << endl; 
67 } 
68 
69 if((choice != "insert") && (choice != "print") && (choice != "length") && (choice != "lookup") && (choice != "remove")) 
70 { 
71 cerr << choice << " is not a legal command, giving up." << endl; 
72 return 1; 
73 } 
74 } 
75 
76 
77 return 0; 
78 } 

基本上現在我只是在嘗試使用插入和選擇。當我輸入插入時,我想創建一個新的視頻對象,並將其放入鏈表中的m_vid點。那麼我想打印打印並做vlist.print()並打印我插入到鏈接列表中的所有視頻。 vlist.cpp的第27行沒有執行Video成員函數print,我不知道爲什麼。我得到了錯誤信息vlist.cpp:27:29:error:'std :: cout < < ptr-> Vlist :: Node :: m_vid-> Video :: print()''中的'operator < <' 。所以即時通訊只是試圖插入視頻到鏈接列表,然後打印出來。發生了什麼問題?如果您需要任何額外的澄清,請讓我知道。

回答

2

VList::print()無效,您不能傳遞給cout

更新:

cout << ptr->m_vid->print(); 

到:

ptr->m_vid->print(); 
+0

哇哈哈,應該已經看到了....非常感謝你 – user1940516