指针指向正在创建的对象
pointers to objects being created
不要提供包含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();
}
}
}
相关文章:
- 使用基类指针创建对象时,缺少派生类析构函数
- 如何创建对象函数指针C++映射?
- C++创建对象数组
- 在 C++ 的 Switch Case 中创建对象后对对象调用方法
- 如何获取在 main() 函数中构造的类的创建对象?
- 基于文件中的条目创建对象
- 错误:创建对象后无法分配区域
- C++ 通过输入创建对象
- 堆还是堆栈用于创建对象?
- 使用 C++ 创建对象数组
- 使用unique_ptr创建对象
- C++递归地在类构造函数中创建对象
- 通过向构造函数其他对象引用页面来创建对象
- ReactiveX (rx) - 在对象上应用可观察对象,而不是在可观察对象中创建对象
- 如何在OSX上正确创建C++对象文件(.o)
- 编译器是否会创建vtable,而不考虑在c++中创建对象
- 创建对象并防止被破坏
- 在C++中,友元类可以从友元类创建对象吗
- 只在堆中创建C++对象
- 创建用户定义的复制构造函数时无法创建对象