【发布时间】:2014-09-25 06:44:47
【问题描述】:
尝试为视频对象创建链接列表以插入、删除、查找、打印。插入函数出错
main.cpp:50:25: 错误: '*' 标记之前的预期主表达式 list.insert(视频*视频); ^
main.cpp
int main (){
23 int counter = 0;
24 string command;
25 Vlist list;
26 Video *video;
29 while (getline(cin, command))
30 {
31 if(command=="insert")
32 {
33 getline(cin, title);
34 getline(cin, url);
35 getline(cin, comment);
36 cin >> length;
37 cin >> rating;
38 cin.ignore();
39
46 list.insert(Video *video);
47 counter++;
48 }
49 else if(command=="remove")
50 {
51 getline(cin, url);
52 getline(cin, comment);
53 cin >> length;
54 cin >> rating;
55 cin.ignore();
56 }
使用传入参数的视频对象创建节点构造函数
videolist.h
5 #include <iostream>
6 #ifndef VLIST_H
7 #define VLIST_H
8 #include <string>
9 #include "video.h"
10
11 using namespace std;
12
13 class Vlist
14 {
15 public:
16 Vlist();
17 void insert(Video *video);
18 void insert_end();
19 void print();
20
21 private:
22 class Node
23 {
24 public:
25 Node(Video *video, Node *next)
26 {
27 m_video=video; m_next=next;}
28 Video* m_video;
29 Node *m_next;
30 };
31
32 Node *m_head;
33
34 };
35 #endif
videolist.cpp
28 void Vlist::insert(Video *video)
29 {
30
31 if(m_head==NULL)
32 {
33 m_head=new Node(video, NULL);
34 }
35 else
36 {
37 Node *ptr=m_head;
38 while(video->get_title()>ptr->m_next -> m_video-> get_title())
39 {
40 ptr=ptr->m_next;
41 }
42 ptr->m_next=new Node(video*, ptr->m_next);
43 }
44
45
46 }
视频.h
5 #ifndef VIDEO_H //if not define
6 #define VIDEO_H
7
8 #include<iostream>
9 #include<string>
10
11 using namespace std;
12
13 class Video
14 {
15 public:
16 Video(string title , string url, string comment, double length, int rating);
17 ~Video();
18 void print();
19 bool longest_length(Video *smallest);
20 bool largest_rating(Video *smallest);
21 bool title_order(Video *smallest);
22 string get_title();
23
24
25 private:
26
27 double m_length;
28 int m_rating;
29 string m_title;
30 string m_comment;
31 string m_url;
32
33 };
34 #endif
35
视频.cpp
64 string Video::get_title()
65 {
66 return m_title;
67 }
【问题讨论】:
-
你真的不需要为编译时错误显示这么多代码。你说错误在main.cpp:50 25,那是行号,对吧?但你没有发布那条线......
-
我修复了代码并放置了代码的右侧部分
标签: c++ pointers linked-list nodes