链接列表功能有问题

时间:2013-09-29 02:37:02

标签: c++ pointers linked-list

我在使用指向视频对象的指针的成员函数时遇到问题。 我的代码如下:

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 }

基本上现在我只是尝试使用插入和选择。当我输入insert时,我想创建一个新的视频对象并将其放入链表Node中的m_vid点。然后我想调用print并执行vlist.print()并打印我插入链接列表的所有视频。 vlist.cpp的第27行没有完成视频成员函数打印,我不知道为什么。我收到错误消息vlist.cpp:27:29:错误:不匹配'operator&lt;&lt;'在'std :: cout&lt;&lt; ptr-&GT; VLIST ::节点:: m_vid-&GT;视频::打印()”。所以,我只想尝试将视频插入链接列表,然后将它们全部打印出来。出了什么问题?如果您需要任何额外的说明,请告诉我。

1 个答案:

答案 0 :(得分:2)

VList::print()无效,您无法转到cout

更新

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

为:

ptr->m_vid->print();