悬赏问题: 所以,这两个 Foo
不是一回事.美好的.第二种形式是在图书馆中给出的.鉴于我无法更改它,我该如何提前声明它?
Bounty question: So, these two Foo
s aren't the same thing. Fine. The second form is given in a library. How do I forward-declare it given that I can't change it?
我一直认为 C 和 C++ 允许重复声明,前提是没有重复定义.然后我在尝试编写扩展 C 库的 C++ 代码时遇到了这个问题.
I always thought C and C++ allowed repeated declarations provided that there were no repeated definitions. Then I came across this problem when trying to write C++ code which extends a C library.
struct Foo;
typedef struct {} Foo;
出现以下错误:
'struct Foo' 先前声明为 'struct Foo'
'struct Foo' has a previous declaration as 'struct Foo'
我要提前申报,该死!这里有什么问题吗?
I want to forward-declare, darn it! What's wrong here?
typedef-ing anonymous struct 是一种早于 C++03 的实践,主要面向保持与 C99 之前的编译器的兼容性.
typedef-ing anonymous struct is a practice that pre-dates C++03 and is mainly oriented to retain compatibility with pre-C99 compilers.
鉴于这是 2011 年,而且 C++ 和 C 都发生了变化,我想知道为什么没有更新版本的此类库!
Given that this is 2011, and that both C++ and C are changed, I wonder why there is no more up-to-date version of such a library!
如果它不再处于开发阶段,你就不能离开",而只能生存"并改变它就是这样做的方式.如果仍在部署中,请将问题提交给开发团队.
If it is not in development anymore, you cannot "leave", but just "survive" and change it is the way to do that. If still in deployment, submit the issue to the development team.
如果您需要解决方法,请考虑结构可以继承.所以,写一个像
If you need a workaround, consider that struct can inherit. So, write a forward declaration like
struct MyFoo;
并将其定义为
#include "old_library.h"
struct MyFoo: public Foo {};
并且在您的所有代码中,忘记 Foo
并始终使用 MyFoo
.
And in all your code, forget about Foo
and always use MyFoo
.
这篇关于未命名结构的前向声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!