简体   繁体   English

C++中链表中的链表

[英]Linked-list in linked-lists in C++

I ran in soms trouble using a list of lists.我使用列表列表遇到了麻烦。 I'm using a singly linked-list, the linked-list consists of 3 classes (Node, ListIterator and List) these are template classes.我使用的是单链表,链表由 3 个类(Node、ListIterator 和 List)组成,它们是模板类。 The linked-list is tested for every type.链表针对每种类型进行测试。 Class List and other classes are using operator<< to avoid print functions.类 List 和其他类使用 operator<< 来避免打印功能。

I've written some pseudocode to show my problem, the ??我写了一些伪代码来显示我的问题,?? indicate the problem area:指出问题区域:

    class First
    { private:
    string name;    
    public:
        Tentamen (string n) : naam(n) {}
        friend ostream& operator<< (ostream& out, const First& f)
        { return out << "First: " << f.name << endl; }
    };

    class Second
    { private:
        string name;
        List<First> l1;
    public:
        Second(string n) : naam(n){}
        friend ostream& operator<< (ostream& out, const Second& s)
        {
           out << "Second: " << s.name << endl;
    ??     out << s.l1;
           return out;       
        }
    };

    int main()
    { //test lijst template

    //example use of List<T>
        List<string> list2; 
        list2.add( "strand" );     
        list2.add( "zon" );     
        cout << list2; //print

        List<Second> l2;
        l2.add(Student("Jip");
    ??  l2.l1.add("name");

        return 0;
    }

I can make list for every type, every class.我可以为每种类型、每个班级列出清单。 The problem is with l2 of type List, I want to access the l1 of type List in l2.问题出在 List 类型的 l2,我想访问 l2 中 List 类型的 l1。 I'm not able to access l1, I've tried alot with 'friend class' declarations and iterators.我无法访问 l1,我已经尝试了很多“朋友类”声明和迭代器。

For printing I get an error on out << s.l2 in the class Second, operator<<.对于打印,我在类 Second, operator<< 中的 out << s.l2 上出现错误。 'Error: No match for operator<< in out << s.l2::l1' '错误:操作符<< in out << s.l2::l1 不匹配'

I hope its clear enough, if not I'll edit in the Morning (CET).我希望它足够清楚,如果不是,我将在早上(CET)进行编辑。 Thanks for the help.谢谢您的帮助。

As already has been said in the comments you should post the code for the List class template.正如评论中已经说过的,您应该发布List类模板的代码。

That being said my guess is as follows:话虽如此,我的猜测如下:

In your marked line l2.l1.add("name");在您标记的行中l2.l1.add("name"); you try to access the member l1 of l2 which is of type List<Second> .您尝试访问类型为List<Second>l2的成员l1 Seeing that the wrapped class Second has a member l1 I assume you actually want to access this member.看到被包装的类Second有一个成员l1我假设你真的想访问这个成员。 This means you have to navigate to the data element you want to access first and then can operate on this data element.这意味着您必须先导航到要访问的数据元素,然后才能对该数据元素进行操作。

Without giving your code for List we cannot say how to access the data element.如果不提供List的代码,我们就无法说明如何访问数据元素。

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

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