Various prototypes with typedef and #define

I am writing a set of related list functions for a project.
I need to provide flexibility regarding data type in list nodes (int / char)

So, I'm trying to do something like this:

in main.cpp

#define INT_DATA
#include "lists.h"
....

on the lists.h

#ifdef CHAR_DATA
typedef char DATA_TYPE;
#endif
#ifdef INT_DATA
typedef int DATA_TYPE;
#endif

typedef struct lnode {
    DATA_TYPE data;
    struct lnode* next; //points to the next node
} LNODE;

LNODE * createNewListNode(DATA_TYPE data, LNODE* next);
....

but all this does not seem to work ... I do not want to write duplicate code when only prototypes are different.

What am I doing wrong? Is it possible?

NOTE

This exercise, I can’t use classes, I can’t use any standard libraries

+3
source share
2 answers

++. . , " " . ... :

template < typename DATA_TYPE >  
struct lnode {
  DATA_TYPE data;
  lnode<DATA_TYPE>* next; //points to the next node
};

:

lnode<char> charNode;
lnode<int> intNode;
+9

. ( : ).

, typedef struct lnode { /* ... */ } LNODE; C, ++.

+2

All Articles