0

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).

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".

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

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

In Network.cpp:

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

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

and then they're defined in main.cpp

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.

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.

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.

Any help here?

Thank you

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • Your constructor isn't doing anything. – chris Dec 23 '13 at 18:36
  • @chris The example above is just one configuration that I tried. I know it's the same in the the header as it is in the .cpp – Carcigenicate Dec 23 '13 at 18:47
  • @AndreyT Ya, that does look like the same issue, but it doesnt really help. The solution given in that is to put them in the same file, but thats not an option. I need both the array and the constant tweak available to multiple files. – Carcigenicate Dec 23 '13 at 18:49

1 Answers1

1

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

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

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

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.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • I removed the extern, and initialized it normally, but now I'm getting multiple definiton warnings – Carcigenicate Dec 23 '13 at 18:59
  • @Carcigenicate: If you've removed `extern`, then there shouldn't be multiple definition errors, unless the compiler thinks it's compiling C rather than C++, or you're including the header more than once without include guards. You could replace `extern` with `static` to make doubly sure it's got internal linkage, but that shouldn't be necessary. – Mike Seymour Dec 23 '13 at 19:02
  • MNETWIDTH and MNETLENGTH are defined in Misc.h (with guards) as int const MNETWIDTH = 5;. Misc.h is then included in several other files. Trying to compile it yeilds a MD error. – Carcigenicate Dec 23 '13 at 19:12
  • Out of nowhere, it suddenly compiled properly. I think it might be the IDE (C4Droid). I was compiling it with the Misc.h file in the foreground, and it gave me then error, but when I switched to main.cpp in the foreground, it ran fine. Oh well, thank you for your help. Much appreciated. – Carcigenicate Dec 23 '13 at 19:14