The first problem is this: I want to create a LinkedList of type_A. type_A will contain a LinkedList of type_B. The below code fails at compile saying: Node_B does not name a type.

The only way i see around this is to create different LinkList.cpp files for each type (such as LL_TypeA.cpp, LL_TypeB.cpp). Then, I could include the LL_TypeB class in the LL_TypeA.cpp file, and instantiate a new variable of LL_TypeB inside a node of Type_A. This seems wasteful. I believe I should have one LinkList.cpp which has the basic code needed to link to a new node of any type. Then i could typecast the node i am creating and link it.

I've tried creating nested classes inside the linkedlist class: class linklist{class Type_A{} class Type_B{}} with Type_A/B having it's own struct's for their "nodes".. but i don't know how to reference a struct within nested classes. if i could do this, then maybe i could create a dummy class and a dummynode and then in the cpp file typecast the class and then create a new node and typecast that to be either Type_A or Type_B.. could that work?

Is there a better way?

#ifndef __LINKEDLIST_H
#define __LINKEDLIST_H
#include <string>

using namespace std;

class linklist
{
     private:

     struct Node_A
         {
              string data;
             Node_A *link;
             Node_B myNode_B
         }*A;

     struct Node_B
         {
             string data;
             Node_B *link;
         }*B;
}

Edited 4 Years Ago by reallyslick

This article has been dead for over six months. Start a new discussion instead.