Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forward declaring anonymous structure from C in c++

I have a third party C library with an anonymous structure similar to

    typedef struct
     {
      ...
      }A;

This file is automatically generated through a program, thus can change based on something

Now in a C++ project I accept argument of above type, for example as

  void foo(const A & in)
  {
    ...
  }

How do I forward declare A in the header file defining the above function?

I have tried :

 typedef struct A A;

or

 struct A;

and also

 typedef A A;

out of curiosity.

However those result in compile time error of redefining the type. Thanks

like image 566
user6386155 Avatar asked Aug 10 '16 13:08

user6386155


1 Answers

Sadly you're out of luck.

A is a typedef for an untagged struct. And you can't forward declare that since you can't refer directly to that struct.

Can you change the generator so it outputs typedef struct A { ... } A;? Then it would compile well in both C and C++, and you could forward declare that in C++.

Otherwise you have two options: 1. #include the file containing the struct declaration. 2. Copy the struct by hand. Obviously (1) is much more preferable.

like image 148
Bathsheba Avatar answered Sep 20 '22 08:09

Bathsheba