【发布时间】:2016-09-26 04:08:35
【问题描述】:
不要提供包含矢量的答案!
我对指针的理解很差,我研究的每个教程只会让我更加困惑。
目前我的程序可以采用“插入”命令。然后它将读取名称、url 和评论的字符串、长度的 double 和评级的 int。然后它将创建一个对象。
我需要做的是创建一个指针并将其作为参数传递,以便在使用来自不同类的函数时创建指向对象的指针列表。
我该怎么做:
创建指向在 main 中创建的对象的指针?
将指向正在创建的对象的指针作为参数传递给在 vlist 类中创建和定义的函数?
这是我的代码
main.cpp
#include <iostream>
#include <string>
#include "vlist.h"
using namespace std;
#include "video.h"
int main(){
string command;
string comment, url, name; // first three getlines
double length; // The length for the movie
int rating;
while (getline(cin,command)){
if (command == "insert"){
getline(cin, name);
getline(cin, url);
getline(cin, comment);
cin >> length;
cin >> rating;
cin.ignore();
//Video *vid
Video = new Video(name, url, comment, length, rating);
List list;
list.insert();
}}
return 0;
}
视频.h
#ifndef VIDEO_H
#define VIDEO_H
#include<iostream>
#include<string>
using namespace std;
class Video{
public:
Video(string name, string url, string comment,
double length, int rating);
void print();
private:
string m_comment;
string m_url;
string m_name;
double m_length;
int m_rating;
};
void list_length(Video *video[], int num_vids);
#endif
视频.cpp
#include<iostream>
#include "video.h"
using namespace std;
Video :: Video (string name, string url, string comment,
double length, int rating){
m_comment = comment;
m_url = url;
m_name = name;
m_length = length;
m_rating = rating;
}
void Video :: print (){
cout << m_name << ", " << m_url << ", " << m_comment << ", "
<< m_length << ", ";
for(int count = 0; count < m_rating; ++count){
cout << "*";
}
cout << endl;
}
vlist.h
#ifndef VLIST_H
#define VLIST_H
#include<iostream>
#include<string>
using namespace std;
class List{
public:
List();
void insert (Video *video);
private:
class Node{
public:
Node(string name, string url, string comment,
double length, int rating, Node *next){
m_name = name;
m_url = url;
m_comment = comment;
m_length = length;
m_rating = rating;
m_next = next;
}
string m_name, m_url, m_comment;
double m_length;
int m_rating;
Node *m_next;
};
Node *m_head;
};
#endif
vlist.cpp
#include<iostream>
#include<string>
#include "vlist.h"
using namespace std;
List :: List (){
m_head = NULL;
}
void List :: insert(Video *video){
m_head = new Node (name, url, comment, length, rating, m_head);
}
【问题讨论】:
标签: c++ pointers object infinite-loop