-1

The code is given below is not allowed to compile because of struct's order. song_node struct has playlist variable and playlist struct has song_node variable.

This code is runned on Visual Studio or gcc compile.

struct song_node {
    song* data;
    song_node* next;
    song_node* prev;
    playlist* parent;
};

struct playlist {
    int songnumber;
    char* name = new char[LNAME_LENGTH];
    song_node* head;
    playlist* next;
    playlist* prev;
};

I am new on Xcode. What is the problem in this code?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
tgbzkl
  • 67
  • 1
  • 10

2 Answers2

1

Consider a forward declaration for the class playlist:

struct playlist; //Here

struct song_node {
    song* data;
    song_node* next;
    song_node* prev;
    playlist* parent;
};

struct playlist {
    int songnumber;
    char* name = new char[LNAME_LENGTH];
    song_node* head;
    playlist* next;
    playlist* prev;
};

Then the compiler knows about playlistand you can use it in song_node even though it's not implemented (yet).

Stack Danny
  • 7,754
  • 2
  • 26
  • 55
1

You need to do a forward declaration.

struct playlist;

struct song_node {
    song* data;
    song_node* next;
    song_node* prev;
    playlist* parent;
};

struct playlist {
    int songnumber;
    char* name = new char[LNAME_LENGTH];
    song_node* head;
    playlist* next;
    playlist* prev;
};

The compiler needs to know that there is something like a playlist when you want to use it within song_node. By forward-declaring playlist you tell the compiler that such an object exists.

user2393256
  • 1,001
  • 1
  • 15
  • 26