global typedef

I try to make a linked list class for my school project and got some problem with typedef thing.


MY HEADER FILE LOOKS LIKE (list.h):

struct node;
struct dataTypeList;
typedef dataTypeList itemListType;

class list
{
  public:
    list();
    ~list();
    void getItem(itemListType &newItem);

...
};


MY CPP FOR THE HEADER FILE (list.cc):

include "list.h"

struct node
{
   ...
};

struct dataTypeList
{
   int value;
   int multiple;
};

...


MY PROGRAM FILE (try.cc):

include "list.h"

struct dataTypeList
{
   int value;
   int multiple;
};
...

the problem is: do I have to put the struct statement in both class (my program class and class implementation for my header file). I need to make 2 different programs which will use that linked list container, the first program will use that struct while the data for the other only an integer. If I put the struct in the list.cc, my second program will be nasty because I have to use that struct while I don;t need it.

is there any way I can make the itemListType in list.h to be default to int (ie. typedef int itemListType). And if I declare the typedef in try.cc (ie. typedef dataListType itemListType), the itemListType in list.h and list.cc will be dataListType (but I don't want to declare the struct in the list.cc)?

BTW, I'm using CC compiler for unix.

nozzle99Asked:
Who is Participating?
 
nietodConnect With a Mentor Commented:
The problem is that you have code in list.cc that depends on this #define.  the define works well for prog1.cc and prog2.cc because they define the #define when needed and not when not needed.  but list.cc never has the #define, and sometimes it needs it.

One "patch" would be to get rid of list.cc and simply place its contents in list.h.  That woudl work because list.h "sees" the define when it is included into progr1.cc or prog2.cc.   (or doesn't "see" the define, when it shoudln't).

There are a few other "patches" possible.  but I really don't like them.  The ideal solution is to use templates.  Maybe you should start learning about them.  Do you have a book that discusses them?
0
 
mnewton022700Commented:
The struct statement should be in list.h. You can't do what you are trying to do. In order to create the two different lists that you want you will need to either use a template class or create two different list classes.

Actually, I just has a thought. If your programs are in two separate directories you could define your list type in a header file (lets call it data.h). Your list class header file would include data.h. Your two programs would each have their own copy of list.h, list.cpp, and data.h. The list files would be identical, only the data.h files would be different.

I think that a template is probably the best solution though.
0
 
nietodCommented:
Templates are the ideal solution for this, but probably bejond your current level of expertise.

Another posibility is to use conditional compilation.  For example you woudl place the following code in a header some where.  (I'm not sure I understand the maining all your files etc, so I don't know where).

#ifndef LISTITEMDEFINDED
   struct dataTypeList
   {
      int value;
      int multiple;
   };
   typedef dataTypeList itemListType;
#endif

Then if you want to use the structure in the linked list, and don't need to do anything special.  If you want to use another type in the linked list, you just need to place a like like

#define LISTITEMDEFINED

before the include file that has the text above.  You also need to create an appropriate typedef for the itemListType.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
DhrubajyotiCommented:
you should have your structure in header file

 your header file should look like this
MY HEADER FILE LOOKS LIKE (list.h):

struct node;
struct dataTypeList
{
   int value;
   int multiple;
};

#ifdef LSTM
typedef dataTypeList itemListType;
#elif
typedef dataTypeList int;
# endif




class list
{
  public:
    list();
    ~list();
    void getItem(itemListType &newItem);

....
};

now in your c file you have to define

#define LSTM
if you want to use the structure

and you have to include the header file.
  you don't have to write any struct statement in c file.


0
 
DhrubajyotiCommented:
Dhrubajyoti changed the proposed answer to a comment
0
 
DhrubajyotiCommented:
i made mistake in typedef syntex
this will work
you should have your structure in header file

 your header file should look like this
MY HEADER FILE LOOKS LIKE (list.h):

struct node;
struct dataTypeList
{
   int value;
   int multiple;
};

#ifdef LSTM
typedef dataTypeList itemListType;
#elif
typedef int itemListType;
# endif




class list
{
  public:
    list();
    ~list();
    void getItem(itemListType &newItem);

....
};

now in your c file you have to define

#define LSTM
if you want to use the structure

and you have to include the header file.
  you don't have to write any struct statement in c file.


0
 
nietodCommented:
Dhrubajyoti, that is exactly what I proposed.

I'll assume you are new to EE and don't realize that we don't post other expert's solutions as our own answers.
0
 
nozzle99Author Commented:
I still don't understand where to put the #define command. Suppose that you have 4 files, list.h, list.cc, prog1.cc, and prog2.cc. prog1.cc will use the struct and prog2.cc not use the struct.

I have try to put the define in prog1.cc, but it won't compile; If I put the define in list.cc, it compile properly. But the problem is the list.h and list.cc must be the same for both programs.

I compile use command CC -o prog1 list.cc prog1.cc in unix; even if I change the command become CC -o prog1 prog1.cc list.cc is still not working.

From my discovery, in list.h, the define stuff is not working if I put define in prog1.cc (it still not defined in list.h). Any other suggestions???

BTW, do I only need to write
#define SOMETHING
in the cc file??
0
 
mnewton022700Commented:
What is the compile error? Let us see your code again.
0
 
nietodCommented:
>>  prog1.cc will use the struct

 and

>>I have try to put the define in prog1.cc,
>> but it won't compile;

that is backwards.  If prog1.c wants to use the struct, it shoudl not have the define.

>> If I put the define in list.cc, it compile properly
It should not be there.  it should be in the program that uses the list (or not in the program that uses the list.)

>> BTW, do I only need to write
>> #define SOMETHING
>> in the cc file??
Yes.  but the SOMETHING must be th right thing.  if the #if is looking for "LISTITEMDEFINDED ", then the SOMETHIGN  must be "LISTITEMDEFINDED".
0
 
nozzle99Author Commented:
OK here is some sample of my program...

PROG1.CC
#include <iostream.h>
#include "list.h"

int main()
{
   listClass list;
   dataTypeList temp;
   temp.value=1;
   temp.multiple=1;
   list.append(temp);
}

PROG2.CC
typedef int listItemType;
#define LISTITEMDEFINED

#include <iostream.h>
#include "list.h"

int main()
{
   listClass list;
   int temp=1;
   list.append(temp);
}

LIST.H
struct node;
#ifndef LISTITEMDEFINED
struct dataTypeList
{
    int value;             
    int multiple;       
};      
typedef dataTypeList listItemType;
#endif

typedef node *ptrType;
 
class listClass
{
public:
    listClass();             
    ~listClass();            
    void append(listItemType newItem);

private:
    ptrType curr;
    ptrType head;
    ptrType tail;      
};

LIST.CC
#include "list.h"      // header file
#include <stddef.h>      // for NULL

struct node            
{
    listItemType data;      
    node *next;            
};

listClass::listClass()
{
    curr = NULL;
    head = NULL;
    tail = NULL;
}  

listClass::~listClass(){}


void listClass::append(listItemType newItem)
{
    ptrType temp = new node;
    temp -> data = newItem;
    temp -> next = NULL;

    if (tail == NULL)
    {  
        tail = temp;
        head = tail;
        curr = tail;
    }
    else
    {
        tail -> next = temp;
        tail = temp;
    }
}


my compile command...
first program:
  CC -o prog1 list.cc prog1.cc
second program:
  CC -o prog2 list.cc prog1.cc


first program is compiled perfectly, but the second one is not, the error is :

>CC -o prog2 list.cc prog2.cc
list.cc:
prog2.cc:
Undefined            first referenced
  symbol                        in file
listClass::append(int)              prog2.o
ld: fatal: Symbol referencing errors. No output written to prog2


from what I see, I think the list.h still not recognize the LISTITEMDEFINED although it's already defined in prog2.cc. If I put the define command in list.cc it compile perfectly for prog2.cc but not prog1.cc. is it probably because of the compiler itself?
0
 
mnewton022700Commented:
Try this instead:

PROG1.CC
#include <iostream.h>

#define DATATYPELIST
#include "list.h"

int main()
{
   listClass list;
   dataTypeList temp;
   temp.value=1;
   temp.multiple=1;
   list.append(temp);
}

PROG2.CC
#include <iostream.h>
#include "list.h"

int main()
{
   listClass list;
   int temp=1;
   list.append(temp);
}

LIST.H
struct node;
#ifdef DATATYPELIST
struct dataTypeList
{
    int value;
    int multiple;
};
typedef dataTypeList listItemType;
#else
typedef int listItemType;
#endif

....

This way listItemType will always be defined.
0
 
nozzle99Author Commented:
Thanks anyone for the comments and suggestions.
I think I'm gonna use struct for both program, but for the second one I only use 1 variable in that struct.
I will start learning about templates as the suggestion from nietod, but I will not use it in this program.

Thank You
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.