简体   繁体   English

C ++创建链接列表的链接列表

[英]C++ creating a linked list of linked lists

So, this is a part of my "linked_list.h" header: 因此,这是我的“ linked_list.h”标头的一部分:

template <typename T>
class Linked_list {
public:
  Linked_list();
  ~Linked_list();

  void add_first(const T& x);
  //...
};

And a part of my implementation: 而我的实现的一部分:

template <typename T>
line 22: void Linked_list<T> :: add_first(const T& x)
{
  Node<T>* aux;
  aux = new Node<T>;
  aux->info = x;
  aux->prev = nil;
  aux->next = nil->next;
  nil->next->prev = aux;
  nil->next = aux;
}

and I'm trying to make a linked list of linked lists of strings and add strings in one linked list of my linked list, like this: 并且我正在尝试创建一个字符串链接列表的链接列表,并将字符串添加到我的链接列表的一个链接列表中,如下所示:

Linked_list<Linked_list<string> > *l;
l[0]->add_first("list");
//also I've tried l[0].add_first("list"); but it didn't work either

Thank you. 谢谢。

Later edit: When I try l[0]->add_first("list") these are the errors: 以后编辑:当我尝试l [0]-> add_first(“ list”)时,这些是错误:

main.cc: In function ‘int main()’:
main.cc:22:22: error: no matching function for call      to‘Linked_list<Linked_list<std::basic_string<char> > >::add_first(const char [4])’
main.cc:22:22: note: candidate is:
In file included from main.cc:6:0:
linked_list.cc:28:6: note: void Linked_list<T>::add_first(const T&) [with T = Linked_list<std::basic_string<char> >]
linked_list.cc:28:6: note:   no known conversion for argument 1 from ‘const char [4]’ to ‘const Linked_list<std::basic_string<char> >&’

Later later edit: It worked finally, thank you for the ideas: I did just this and it's okay now: 稍后再进行编辑:最终成功了,谢谢您的想法:我就是这样做的,现在可以了:

Linked_list<Linked_list<string> > l;
l[0].add_first("list");

And it works :D. 它的工作原理:D。 Thanks again ! 再次感谢 !

Neah..actually it doesn't work.. 恩..实际上是行不通的..

You have created a pointer to linked list and have never pointed it to existent element. 您已经创建了指向链表的指针,并且从未将其指向存在的元素。 Either use new to allocate dynamic memory or use an object instead of a pointer. 使用new分配动态内存,或者使用对象代替指针。

Like so: 像这样:

Linked_list<Linked_list<string> > *l = new Linked_list<Linked_list<string> >();

Or like so: 或者像这样:

Linked_list<Linked_list<string> > l;

The fact that you are using the operator[] may mean you meant to use an array so probably you will have to go with first version. 您正在使用operator[]的事实可能意味着您打算使用数组,因此可能必须使用第一个版本。

you're trying to access uninitialized pointer. 您正在尝试访问未初始化的指针。 use 采用

Linked_list<Linked_list<string> > lol;
Linked_list<string> los;
los.add_first("list");
lol.add_first(los);

or 要么

Linked_list<Linked_list<string> > *p = new Linked_list<Linked_list<string> >; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM