Right now, I'm learning data structures and specifically linked lists right now along with templates. I'm in a bit of a "pickle" right now though... I have a few structs that I want to store in the linked list. There's the base struct and a few derived structs.
Is there anyway to create a linked list template that can hold all of these? I've tried by declaring the list with the name of the base struct but when I try and use values from the derived classes it gives me an error saying those values aren't members of <base class>. Where am I going wrong? Is this not possible?
I've looked at run-time polymorphism and I'm guessing that's what's going on here. I have a few values in base struct, with some additional values in the derived struct. When I try and load the derived struct into the list, it claims "derivedVariable not member of BaseClass"...
This is how I'm setting everything out currently:
I was declaring the linked list in a totally different class as:
And in one of the member functions of this new class I create an object of one of the derived classes by:
In my linked list class I add a new Node by using one of its member functions, add(dataType data*). Does that look good?
Then back to the member function where I create a pointer of the derived object, to add it to the list, I use:
I've tried making that initial LinkedList object creation with a pointer, but that stops the add() function from working (gives me an error, saying the data needs to be part of a class, struct etc)...
Any thoughts? Thanks.
Is there anyway to create a linked list template that can hold all of these? I've tried by declaring the list with the name of the base struct but when I try and use values from the derived classes it gives me an error saying those values aren't members of <base class>. Where am I going wrong? Is this not possible?
I've looked at run-time polymorphism and I'm guessing that's what's going on here. I have a few values in base struct, with some additional values in the derived struct. When I try and load the derived struct into the list, it claims "derivedVariable not member of BaseClass"...
This is how I'm setting everything out currently:
I was declaring the linked list in a totally different class as:
Code:
LinkedList<Base> list;
And in one of the member functions of this new class I create an object of one of the derived classes by:
Code:
Derived* derived= new Derived();
In my linked list class I add a new Node by using one of its member functions, add(dataType data*). Does that look good?
Then back to the member function where I create a pointer of the derived object, to add it to the list, I use:
Code:
list.add(derived);
I've tried making that initial LinkedList object creation with a pointer, but that stops the add() function from working (gives me an error, saying the data needs to be part of a class, struct etc)...
Any thoughts? Thanks.