指向正在创建的对象的指针

时间:2016-09-26 04:08:36

标签: c++ pointers object infinite-loop multiple-files

不要提供包含矢量的答案!

我对指针的理解非常差,而且我研究的每个教程都让我更加困惑。

目前我的程序可以采用'insert'命令。然后它将读取名称,url和comment的字符串,长度的double和评级的int。然后它会成为一个对象。

我需要做的是创建一个指针并将其作为参数传递,以便在使用来自不同类的函数时生成指向该对象的指针列表。

我如何:

创建指向在main?

中创建的对象的指针

将指针作为参数创建的对象传递给在vlist类中创建和定义的函数?

这是我的代码

的main.cpp

private void timer1_Tick(object sender, EventArgs e)
{
      button1.BackColor = Color.Black;
      timer1.stop();
}

video.h

#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.cpp

#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

vlist.h

#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.cpp

#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

1 个答案:

答案 0 :(得分: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();
        }
    }
}