指针指向正在创建的对象

pointers to objects being created

本文关键字:创建 对象 指针      更新时间:2023-10-16

不要提供包含vector的答案!

我对指针的理解很差,我研究的每一个教程只会让我更困惑。

目前我的程序可以接受'insert'命令。然后,它将读取字符串以获取名称、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;
 }

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

你混淆了什么是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();
        }
    }
}