Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was doing COM in plain C[^] tutorial. I have the following header file:
C++
#ifndef EXAMPLE_H
#define EXAMPLE_H

#define BUF_SIZE 80

typedef long SetStrPtr(IExample *, char *);
typedef long GetStrPtr(IExample *, char *, long);

typedef struct {
    SetStrPtr * SetString;
    GetStrPtr * GetString;
} IExampleVtbl;

typedef struct {
    IExampleVtbl * lpVtbl;
    DWORD          count;
    char           buffer[BUF_SIZE];
} IExample;
#endif


Now this gives me a compile error. Because..

1. to declare the structure IExample, I need to first declare IExampleVtbl
2. ..which needs the declaration of SetStrPtr and GetStrPtr
3. ..which needs the declaration of IExample

Now as you can see, it's a cyclic dependency. How to get through this?
Posted
Updated 16-Feb-12 1:15am
v2

try this:
C
#ifndef EXAMPLE_H
#define EXAMPLE_H
 
#define BUF_SIZE 80

struct _IExample;
typedef struct _IExample IExample;

typedef long SetStrPtr(IExample *, char *);
typedef long GetStrPtr(IExample *, char *, long);

 
typedef struct {
    SetStrPtr * SetString;
    GetStrPtr * GetString;
} IExampleVtbl;
 
struct _IExample{
    IExampleVtbl * lpVtbl;
    DWORD          count;
    char           buffer[BUF_SIZE];
};

#endif
 
Share this answer
 
v2
Comments
krumia 16-Feb-12 11:52am    
Yay! worked! Grazie signor Pallini. ;) 5ed!

But, there should be a slight change. typedef struct _Example IExample; should be typedef struct _IExample IExample;
CPallini 16-Feb-12 15:34pm    
Grazie!
Sorry for the mistake, now it should be fixed.
It's nasty, but the only way to do it is you change the IExample pointers to void pointers:
C#
typedef long SetStrPtr(void *, char *);
typedef long GetStrPtr(void *, char *, long);

It will work perfectly happily because all pointers are the same length, but it won't be strongly typed.
 
Share this answer
 
Comments
krumia 16-Feb-12 11:56am    
Thanks! But signor Pallini had a strongly typed answer :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900