【问题标题】:pointers to objects being created指向正在创建的对象的指针
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 我不确定你的意思我试过了,我可能对你所说的有错误的想法。
    • 你不明白哪一部分?你尝试了哪一部分?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    • 2012-11-30
    • 2018-02-25
    相关资源
    最近更新 更多