I need to access members of a linked list class inside of a linked list. I can manage my Artist list okay, but cannot set int x in my SongList class. I've tried setting it with *(temp->songHead).x = 5;, *temp->songHead.x = 5;, *(temp->songHead)->x = 5;, and *temp->songHead->x = 5;.

When I compile it, I get the error:

invalid use of incomplete type 'struct songList'

How can I set int x?

class SongList{
        int x;
        void add(int x);
        struct Song{
            char *name;
            int minutes;
            int seconds;
            int views;
            int likes;
            Song *next;
        Song *head;

class LinkedList{

        void test(int x);
        void add(char ch);
        bool find(char ch);
        bool del(char ch);
        void list();

        struct Artist{
            char character;
            Artist *next;
            struct songList *songHead;
            SongList ptr;
        Artist *head;

// Code to set int x
void LinkedList::test(int x){
    struct Artist *temp;
    temp = head;
    *(temp->songHead).x = 5;

1 Answers

Alan Birtles On

C++ doesn't require you to add struct in front of declarations of variables containing structs. Adding it allows you to use undefined types in your variable declarations.

If you remove struct you'll quickly see the true cause of your error:

songList *songHead;

Will give an error something like this (this is from clang, other compilers may not be as helpful):

error: unknown type name 'songList'; did you mean 'SongList'?

Your access to songHead is also incorrect:

*(temp->songHead).x = 5;

This is equivalent to:

*(temp->songHead.x) = 5;

What you actually want is:

(*temp->songHead).x = 5;

Or more simply:

temp->songHead->x = 5;