"数组边界不是一个整数前面冠以']'令牌QUOT;使用多个文件时 [英] "array bound is not an integer constant before ']' token" when using multiple files

查看:354
本文介绍了"数组边界不是一个整数前面冠以']'令牌QUOT;使用多个文件时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的神经网络的问题。
我存储组成的网络中的阵列,其尺寸通调整在编译时设置(在调整都是常数)中的节点。

I'm having an issue with my neural net. I'm storing the nodes that make up the network in an array, whose dimensions are set via-tweaks at compile time (the tweaks are all const).

在code工作得很好,直到我决定把它分割成多个文件,但即使有一个extern声明,但它仍然说:数组边界不是一个整数之前']'令牌不变。

The code worked fine until I decided to split it up into multiple files, but even with a extern declaration, it still says that "array bound is not an integer constant before ']' token".

现在,这是设置:
在Network.h:

Right now, this is the set-up: In Network.h:

struct Network {
Node nodes [MNETWIDTH] [MNETLENGTH];
}

在Network.cpp:

In Network.cpp:

Network::Network () {
Node nodes [MNETWIDTH] [MNETLENGTH];
}

的调整在Misc.h(它包括在Network.h)宣称:

The tweaks are declared in Misc.h (which is included in Network.h):

//////////Genetics Tweaks
extern int const MREPS;
extern int const BEINGSPER;
extern int const MUTRATE
extern double const BTOKEEP;
extern int const DNARANGE;
////////////Genetics Tweaks
////////////Network Tweaks
extern const int MNETWIDTH;
extern const int MNETLENGTH;
////////////End Network Tweaks

,然后他们在main.cpp中定义的

and then they're defined in main.cpp

节点definiton需要在一个首标,因此它可以通过源文件进行访问,而是不断调整不能在报头,因为这样我得到多个声明错误。我以为他们声明为extern会告诉它在其他地方找到的恒定值,但显然不是。

The nodes definiton needs to be in a header so it can be accessed by source files, but the constant tweaks can't be in a header because then I get multiple declaration errors. I thought declaring them as extern would tell it to find the constant value elsewhere, but apparently not.

我想从一个数组到2D矢量进行更改,但变成一个穷凶极恶的混乱,所以我真的很喜欢它,如果我能得到这个工作。

I tried changing it from an array to a 2D Vector, but that turned into an atrocious mess, so I'd really like it if I could get this to work.

我想声明的节点作为类之外的extern在Network.h无任何大小paramenters,然后Network.cpp定义它,但我仍然得到同样的错误。
似乎需要在它被使用的相同的文件中的常数的定义,但在常量变量和节点都在几个文件中是必需的。

I tried declaring nodes as extern in Network.h outside of the class without any size paramenters, then defining it in Network.cpp, but I still get the same error. It seems it needs the constant definition in the same file that it's being used in, but both the const variable and nodes are required in several files.

任何帮助吗?

感谢您

推荐答案

要能用作恒定的前pression,常数变量必须定义,不只是声明。假设你想用它从多个翻译单位,删除的extern 给它内部连接(因此它可以在任何需要它的单位被定义),并添加一个初始化器得到的值。

To be usable as a constant expression, a constant variable must be defined, not just declared. Assuming you want to use it from multiple translation units, remove the extern to give it internal linkage (so it can be defined in any unit that needs it), and add an initialiser to give the value.

此外,删除从构造局部数组声明。我不知道你想,要怎么做,但它不会做任何有用的东西。

Also, remove the local array declaration from the constructor. I'm not sure what you want that to do, but it doesn't do anything useful.

不断调整不能在一个头,因为那时我得到多个错误申报

the constant tweaks can't be in a header because then I get multiple declaration errors

这是因为的extern 给他们外部链接,使他们受到了一个定义规则。

That's because extern gives them external linkage, making them subject to the One Definition Rule.

我以为他们声明为extern会告诉它在其他地方找到的恒定值

I thought declaring them as extern would tell it to find the constant value elsewhere

它告诉编译器的值将在运行时间;但不提供与定义的编译器。该定义是需要一个恒定的前pression使用的值。

It tells the compiler that the value will be available at run-time; but does not provide the compiler with the definition. The definition is needed to use the value in a constant expression.

这篇关于"数组边界不是一个整数前面冠以']'令牌QUOT;使用多个文件时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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