如何访问链接列表中的链接列表中的成员

时间:2019-04-27 01:21:58

标签: c++ class pointers data-structures linked-list

我需要访问链接列表中的链接列表类的成员。我可以管理Artist列表,但是不能在int x类中设置SongList。我尝试使用*(temp->songHead).x = 5;*temp->songHead.x = 5;*(temp->songHead)->x = 5;*temp->songHead->x = 5;进行设置。

编译时出现错误:

  

无效使用不完整类型的“ struct songList”

如何设置int x?

#ifndef LINKED_LIST
#define LINKED_LIST
class SongList{
    public:
        SongList();
        int x;
        void add(int x);
    private:
        struct Song{
            char *name;
            int minutes;
            int seconds;
            int views;
            int likes;
            Song *next;
        };
        Song *head;
};

class LinkedList{
    public:
        LinkedList();
        ~LinkedList();

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

    private:
        struct Artist{
            char character;
            Artist *next;
            struct songList *songHead;
            SongList ptr;
        };
        Artist *head;
};
#endif

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

1 个答案:

答案 0 :(得分:0)

C ++不需要您在包含struct的变量的声明前添加struct。添加它可以让您在变量声明中使用未定义的类型。

如果删除struct,您将很快看到错误的真正原因:

songList *songHead;

会给出这样的错误(这是从clang传来的,其他编译器可能没有帮助):

  

错误:未知类型名称'songList';您是说'SongList'吗?

您对songHead的访问也不正确:

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

这等效于:

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

您真正想要的是:

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

或更简单地说:

temp->songHead->x = 5;
相关问题