前置声明变量? [英] Forward declarations for variables?

查看:129
本文介绍了前置声明变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些C code,我要移植到C ++。在code具有结构

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 ++中,变量的声明的必须与的extern

In C++, a variable declaration must be prefixed with extern:

extern A Unit[10];

// ...

A Unit[10] = { ... };

(注意,在C ++中,你可以忽略开头的结构

这篇关于前置声明变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆