创建指向另一个类中对象的链表时遇到问题
I am having trouble creating a linked list that points to objects in another class
vlist.h
class Vlist
{
public:
Vlist();
~Vlist();
void insert(string title, string URL, string comment, float length, int rating);
bool remove();
private:
class Node
{
public:
Node(class Video *Video, Node *next)
{m_video = Video; m_next = next;}
Video *m_video;
Node *m_next;
};
Node* m_head;
};
main.cpp
int main()
{
....blah blah.... (cin the values)
Array[i] = new Video(title, URL, comment, length, rating);
Vlist objVlist;
objVlist.insert(title, URL, comment, length, rating);
}
vlist.cpp
这就是产生错误的地方
(m_head = new Node(Video, NULL);
该函数的工作是将指向类视频中对象的指针插入列表中。
void Vlist::insert(string title, string URL, string comment, float length, int rating)
{
Node *ptr = m_head;
//list is empty
if(m_head == NULL)
m_head = new Node(Video, NULL);
else
{
ptr = ptr->m_next;
ptr = new Node(Video, NULL);
}
//sort the list every time this executes
}
视频.h
这是我试图使用链表指向的类。
class Video
{
public:
Video(string title, string URL, string comment, float length, int rating);
~Video();
void print();
bool longer(Video *Other);
bool higher(Video *Other);
bool alphabet(Video *Other);
private:
string m_title;
string m_url;
string m_comment;
float m_length;
int m_rating;
};
第一次使用堆栈溢出时,不太确定会发生什么。
尝试更改
m_head = new Node(Video, NULL);
至
m_head = new Node(new Video(title, URL, comment, length, rating), NULL);
这个:
else
{
ptr = ptr->m_next;
ptr = new Node(Video, NULL);
}
并不是将新的CCD_ 1添加到列表的头部的正确方式。需要类似的东西:
ptr = new Node(new Video(title, URL, comment, length, rating), NULL);
ptr->m_next = m_head;
m_head = ptr;
"Video"是一个类的名称
您需要创建一个Video
实例。
void Vlist::insert(string title, string URL, string comment, float length, int rating)
{
Video* v = new Video(title, URL, comment, length, rating);
Node *ptr = m_head;
if(m_head == NULL)
m_head = new Node(v, NULL);
else
{
ptr = ptr->m_next;
ptr = new Node(v, NULL);
}
// ...
}
相关文章:
- 反向给定链表中的K节点
- 如果没有malloc,链表实现将失败
- 文本文件中的单词链表
- 努力将整数转换为链表。不知道我在这里做错了什么
- 链表,反向函数,数据结构
- 使用std::list创建循环链表
- 链表的泛型函数remove()与成员函数remove)
- 为什么不能修改对象中的值?另外,我如何改进此链表?
- 我们可以删除链表中静态内存中的节点吗
- 我在让我的代码输出整个链表时遇到问题.它只是输出我修改过的一些文本文件,而不是整个东西
- 我在双链表上的此打印功能时遇到问题
- 我在显示链表时遇到问题
- 我是C++新手。我在此代码中遇到错误,无法使用 SwapNodes 函数C++创建链表。该函数不交换节点
- 将项目添加到双向链表的背面时遇到问题
- 更新链表时遇到问题
- 实现模板化的单向链表时遇到问题
- 添加到双向链表的背面时遇到问题
- 在模板中创建复制链表的函数时遇到问题
- 删除动态链表时遇到麻烦
- 创建指向另一个类中对象的链表时遇到问题