Forward-declaring an extern struct

2019-09-06 01:17发布

问题:

Before I start:

  • Please note that I do not want to change the major design I'm presenting you here.
  • Feel free to change the title if you think this one doesn't suit well
  • The C++ tag is here because the solution should be equivalent for it as well

I have a header breakpoints.h which declares:

typedef struct BreakpointInfo BreakpointInfo;

/*
 * Iterate all breakpoints known to the Breakpoints service,
 * including breakpoints that are created by other (remote) clients.
 */
typedef void IterateBreakpointsCallBack(BreakpointInfo *, void *);
extern void iterate_breakpoints(IterateBreakpointsCallBack * callback, void * args);

and a corresponding breakpoints.c that defines:

struct BreakpointInfo {
    Context * ctx;
    LINK link_all;

    /*
     * A lot of members ..
     */

    LINK link_hit_count;
};

Now I would like to use this functionallity in another file context.c:

#include <tcf/services/breakpoints.h> // The header file 

extern BreakpointInfo;
//extern struct BreakpointInfo; // Also did not work

void MyIterateBreakpointsCallBack(/* struct */ BreakpointInfo *bpInfo, void *args)
{
    bpInfo->file;
}

void thread_function() 
{
    // ..

    iterate_breakpoints(&MyIterateBreakpointsCallBack, 0);

    // ..
}

But Intelli-Sense tells me:

BreakpointInfo *bpInfo, Error: pointer to incomplete class type is not allowed.

So the question is:

  • Is that even possible without moving the definition of BreakpointInfo into the header file?

I don't want to change that since it is a quite complex program and I don't want to break its' design by doing such things.

It may be possible that I am using it the wrong way but why would there ba a iterate_breakpoints() function available for me to access if I could not access BreakpointInfo?

I hope it is clear what I am asking. Please let me know if you need anything to help me.

Best regards.

回答1:

I don't think it is possible since in MyIterateBreakpointsCallBack you're trying to access members of the type and this requires a complete type at this point.

I am not familiar with the project you're trying to work with but a quick google search hinted me its the tcf.agent project. If this is true, after a quick look in the breakpoints.h file I noticed that there is a get_breakpoint_attribute() that gives you a list of name/value pairs. Maybe that should give you the info you need to implement your MyIterateBreakpointsCallBack().