1

I found an answer for a forward declaration of a typedef in C++ (https://stackoverflow.com/a/2095798). But my situation looks like this:

// a.h
 typedef struct{
    unsigned char bah0 : 1;
    unsigned char bah1 : 1;
    unsigned char bah2 : 1;
    unsigned char bah3 : 1;
    unsigned char bah4 : 1;
    unsigned char bah5 : 1;
    unsigned char bah6 : 1;
    unsigned char bah7 : 1;
 } bah;

// b.h
typedef struct bah;  // Error: using typedef-name 'bah' after struct

 class foo {
   foo(bah b);
   bah mBah;
 };

// b.cpp
 #include "b.h"
 #include "a.h"

 foo::foo(bah b) 
 {
   mBah = b;
 }

and I am not allowd to change anything in a.h and I want to avoid including a.h in b.h. How can I avoid this error or what is the right way to forward declarte bah in this situation?

Thany you! Zlatan

Community
  • 1
  • 1
ge45mue
  • 677
  • 8
  • 23
  • You need to include the file `a.h` in your `b.h`. If you do this, you don't need to include the file in `b.cpp`. – Thomas D. Jan 31 '17 at 08:44
  • Sorry, I want to avoid including a.h in b.h. – ge45mue Jan 31 '17 at 08:47
  • 2
    I would expect `typedef struct bah bah;` to work in practice, but I'm not sure whether it's well-defined. The issue is that the class doesn't *have* a name for you to declare... – Quentin Jan 31 '17 at 08:49
  • 3
    Why the typedef at all? There is no need to do such in C++, just write struct bah { ... } and the forward declaration should work fine by writing struct bah; – hellow Jan 31 '17 at 08:51
  • just look at the related topics :D https://stackoverflow.com/questions/612328/difference-between-struct-and-typedef-struct-in-c?rq=1 – hellow Jan 31 '17 at 08:51
  • 1
    Possible duplicate of [Forward declaration of a typedef in C++](http://stackoverflow.com/questions/804894/forward-declaration-of-a-typedef-in-c) – hellow Jan 31 '17 at 08:52
  • @punkkeks I reckon the double requirement "no changing `a.h`" and "no including `a.h` inside `b.h`" is about hiding a fixed C API in C++ code. – Quentin Jan 31 '17 at 08:52
  • @punkekes: I know I can only use struct bah {...}, but I am not allowed to change anything in a.h – ge45mue Jan 31 '17 at 08:55
  • @Quentin typedef struct bah bah creates error: using typedef-name 'bah' after struct – ge45mue Jan 31 '17 at 09:01
  • @Zlatan [cannot reproduce](http://coliru.stacked-crooked.com/a/ab0f4ca7917a1838). Could you show an example? – Quentin Jan 31 '17 at 09:05
  • You have to include this file. There's no too much choice here. – n. m. could be an AI Jan 31 '17 at 09:10
  • @Punkkeks Thank you, in your related topic I found that this is not possible for anonymous typedef like in a.h. No I have to find a nother way. Thany you! – ge45mue Jan 31 '17 at 09:22

2 Answers2

2

I want to avoid including a.h in b.h

Too bad. b.h depends on the definition of bah from a.h. Your wants are at odds with the rules of the language.

How can I avoid this error

Option 1: Include a.h in b.h. I know you don't want this, but I want to include all options that are available.

Option 2: Don't depend on definitions of a.h in b.h. An example:

// b.h
class foo {
    foo();
};

A following class could be defined with only a forward declaration of bah:

class foo {
    foo(bah* b);
    bah* mBah;
};

However, even this is not possible unless you can forward declare the structure. So this brings us to...

what is the right way to forward declarte bah in this situation?

There is no way to forward declare an unnamed struct. Unless you can modify a.h, you cannot give a tag name for the struct. Assuming that you could change a.h, this is how you would do it:

typedef struct bah { // struct now has the tag name bah
    // ...
} bah;

Since the name of the struct makes the typedef mostly redundant, you could simplify to:

 struct bah {
    // ...
 };

After this addition, you can forward declare:

struct bah;

But forward declaration won't allow you to declare variables of type bah.


PS. Bit fields have no effect on how forward declarations work.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

There are too many dependencies in your solution. Define blah once in common part of the code. You will avoid redefinitions.

I would recommend define blah in a.h as you did and then:

1) include a.h inside b.h. Include b.h inside b.cpp

2) or include a.h before b.h inside b.cpp