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 }

视频.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

视频.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 

主.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 位置。然后我想调用 print 并执行 vlist.print() 并打印我插入到链接列表中的所有视频。vlist.cpp 的第 27 行没有执行 Video 成员函数打印,我不知道为什么。我收到错误消息 vlist.cpp:27:29: error: no match for 'operator<<' in 'std::cout << ptr->Vlist::Node::m_vid->Video::print()' . 所以我只是试图将视频插入链接列表,然后将它们全部打印出来。出了什么问题?如果您需要任何额外的说明,请告诉我。

4

1 回答 1

2

VList::print()是无效的,你不能传递给cout.

更新:

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

到:

ptr->m_vid->print();
于 2013-09-29T02:42:02.690 回答