2016-09-26 121 views
-3

不要提供包含向量的答案!創建對象的指針

我對指針的理解很差,我研究的每個教程只會讓我更加困惑。

目前我的程序可以接受一個'插入'命令。然後,它將讀取字符串的名稱,網址和註釋,長度的兩倍和評分的整數。然後它會產生一個對象。

我需要做的是創建一個指針,並將其作爲參數傳遞,以便在使用不同類中的函數時創建指向該對象的指針列表。

我如何:

創建指向正在創建的對象在主?

將指向正在創建的對象的指針作爲參數傳遞到在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; 
} 

video.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 

video.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); 
} 

回答

0

你混淆了什麼Node是。您曾表示要存儲原始指針像這樣的(注意到一些重排 - 省略不相干的東西,以使其更清晰):

List list; 
while(...) 
{ 
    if(...) 
    { 
     Video video = new Video(name, url, comment, length, rating); 
     list.insert(video); 
    } 
} 

爲了讓這個適合你的意圖很明顯,你Node類應該存儲Video指針,而不是從Video類重複信息:

class Node 
{ 
public: 
    Node(Video *video, Node *next) 
     : m_video(video) 
     , m_next(next) 
    { 
    } 

    Video *m_video; 
    Node *m_next; 
}; 

這使得列表插入,因爲這很容易:

void List::insert(Video *video) 
{ 
    m_head = new Node(video, m_head); 
} 

現在你可以添加一些簡單的列表像print功能:

void List::print() 
{ 
    for(Node *node = m_head; node != nullptr; node = node->m_next) 
    { 
     Video *video = node->m_video; 
     if(video != nullptr) 
     { 
      video->print(); 
     } 
    } 
} 
+0

我不知道你的意思,我想,我可能有你說的話的錯誤觀念。 – Badlittlepiggy

+0

哪部分你不明白?你嘗試過哪部分? – paddy