【发布时间】:2017-01-31 08:43:18
【问题描述】:
我找到了 C++ 中 typedef 前向声明的答案 (https://stackoverflow.com/a/2095798)。但是我的情况是这样的:
// 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;
}
我不允许更改 a.h 中的任何内容,并且我想避免在 b.h 中包含 a.h。在这种情况下,如何避免此错误或转发 declarte bah 的正确方法是什么?
谢谢你! 兹拉坦
【问题讨论】:
-
您需要在您的
b.h中包含文件a.h。如果这样做,则无需将文件包含在b.cpp中。 -
抱歉,我想避免在 b.h 中包含 a.h。
-
我希望
typedef struct bah bah;在实践中工作,但我不确定它是否定义明确。问题是该类没有有名称供您声明... -
为什么要使用 typedef?在 C++ 中不需要这样做,只需编写 struct bah { ... } 并且通过编写 struct bah 前向声明应该可以正常工作;
标签: c++ typedef forward-declaration unnamed-class