变量的转发声明? [英] Forward declarations for variables?
本文介绍了变量的转发声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些C代码,我必须移植到C ++。代码有一个结构
I have some C code that I have to port to C++. The code has a structure
struct A {
...
struct A * myPtr;
}
现在两个全局数组声明和初始化如下:
And now two global arrays are declared and initialized like this:
//Forward declaration of Unit
struct A Unit[10];
struct A* ptrUnit[2] = { Unit, Unit+7 };
struct A Unit[10] = { { .., &ptrUnit[0] },
... };
现在,虽然这在C中工作得很好,但是它在C ++(变量重新声明)中给出错误。
不允许在C ++中向前声明变量吗?
Now while this works fine in C, it gives an error in C++ (variable redeclared). Aren't variables allowed to be forward-declared in C++?
推荐答案
在C ++中,变量声明必须以 / code>:
In C++, a variable declaration must be prefixed with extern
:
extern A Unit[10];
// ...
A Unit[10] = { ... };
(注意在C ++中可以省略前面 struct
。)
(Note that in C++ you can omit the leading struct
.)
这篇关于变量的转发声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文