Solved

global typedef

Posted on 2000-04-25
13
840 Views
Last Modified: 2012-05-04
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.

0
Comment
Question by:nozzle99
  • 4
  • 3
  • 3
  • +1
13 Comments
 
LVL 3

Expert Comment

by:mnewton022700
Comment Utility
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
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
 
LVL 1

Expert Comment

by:Dhrubajyoti
Comment Utility
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
 
LVL 1

Expert Comment

by:Dhrubajyoti
Comment Utility
Dhrubajyoti changed the proposed answer to a comment
0
 
LVL 1

Expert Comment

by:Dhrubajyoti
Comment Utility
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
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:nozzle99
Comment Utility
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
 
LVL 3

Expert Comment

by:mnewton022700
Comment Utility
What is the compile error? Let us see your code again.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>>  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
 

Author Comment

by:nozzle99
Comment Utility
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
 
LVL 3

Expert Comment

by:mnewton022700
Comment Utility
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
 
LVL 22

Accepted Solution

by:
nietod earned 150 total points
Comment Utility
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
 

Author Comment

by:nozzle99
Comment Utility
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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now