简体   繁体   中英

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. The linked-list is tested for every type. Class List and other classes are using operator<< to avoid print functions.

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. I'm not able to access l1, I've tried alot with 'friend class' declarations and iterators.

For printing I get an error on out << s.l2 in the class Second, operator<<. 'Error: No match for operator<< in out << s.l2::l1'

I hope its clear enough, if not I'll edit in the Morning (CET). Thanks for the help.

As already has been said in the comments you should post the code for the List class template.

That being said my guess is as follows:

In your marked line l2.l1.add("name"); you try to access the member l1 of l2 which is of type List<Second> . Seeing that the wrapped class Second has a member l1 I assume you actually want to access this member. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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