I am trying to create a dynamic array of type 'T' that contains an array of type 'X'. In order to access attributes of T from X, I tried creating a pointer in struct X that points to T. This is the code I have:
struct WireSeg; // Forward declaration
struct Track { // Type X in above description
int trackNum;
bool used;
WireSeg* parentWireSeg;
};
struct WireSeg { // Type T in above description
int xCoord;
int yCoord;
char flag;
char orientation;
Track* track;
};
typedef WireSeg* WireSegPtr
int main (void) {
WireSegPtr wireSeg;
wireSeg = new WireSeg[5];
for (int i =0; i<5; i++) {
wireSeg[i].track = new Track[3];
for (int j =0; j<3; j++) {
wireSeg[i].track[j].parentWireSeg = wireSeg[i];
}
}
}
I get the compile error:
error: cannot convert ‘WireSeg’ to ‘WireSeg*’ in assignment
I don't get it. parentWireSeg
has been declared as a WireSeg
type pointer and wireSeg[i]
is also an element in the wireSeg
array and is a pointer (isn't it?).
I tried playing around with it and declared parentWireSeg
to be of type WireSeg
:
struct Track {
int trackNum;
bool used;
bool flag;
WireSeg parentWireSeg;
};
This gave me error:
‘struct Track’ has no member named ‘parentWireSeg’.
This makes no sense to me either since struct Track
does have parentWireSeg
as an element! Can someone please explain this? Am I not allowed to have a pointer in Track
that points to WireSeg
?
I can probably use inherited classes (can I?) for this but I would prefer if someone told me what is wrong with my method?