1

I am currently working on a little program for the Raspberry Pi. It involves some 7 segment displays. To be able to write more programs for this display I decided to extract the code that directly communicates with the GPIO's to a seperate .cpp and .h file.

Since the number of digits is variable I used a variable for that. The digits themselves are stored in a array consiting out of 8 bit integers.

This is what my setup looks like:

7_segment.h:

extern const uint8_t numDigits;
extern uint8_t digits[];

7_segment.cpp:

#include "7_Segment.h"

uint8_t digits[numDigits] = {0x00}; // Line 7

And the file with the "actual" program:
clock.cpp:

#include "7_Segment.h"

const uint8_t numDigits = 4;

When I execute

g++ clock.cpp 7_segment.cpp -o clock -std=c++0x -lwiringPi

I get this output:

7_segment.cpp:7:27: error: array bound is not an integer constant before ‘]’ token

What can I do to resolve this?

BrainStone
  • 3,028
  • 6
  • 32
  • 59
  • possible duplicate of [initialize array with constant number does not work](http://stackoverflow.com/questions/16866267/initialize-array-with-constant-number-does-not-work) – IdeaHat Sep 11 '14 at 16:22
  • 1
    (googling your exact error): http://stackoverflow.com/questions/20749238/array-bound-is-not-an-integer-constant-before-token-when-using-multiple-fi – IdeaHat Sep 11 '14 at 16:23
  • A side note: you're basically trying to use the `extern` variable as part of the API for your `7_Segment` code. The API says "you must define this variable." I don't think using `extern` objects is a very good way to expose API. – Joseph Mansfield Sep 11 '14 at 16:24

1 Answers1

1

The numDigits=4 is defined in the clock.cpp. The 7_segment.cpp has no idea about the size of array digits[]. Array sizes need to be compile-time constants, so you'll need to put the actual number in 7_segment.cpp or as a compile-time constant in 7_segment.h instead.

Colin D Bennett
  • 11,294
  • 5
  • 49
  • 66
Tianyun Ling
  • 1,045
  • 1
  • 9
  • 24
  • That's what I'm trying to avoid. Since later on I do not want to modify the 7_segment files. Only clock.cpp or other files that include 7_segment.h – BrainStone Sep 11 '14 at 16:25
  • Then put the definition of `numDigits` in another header that is included by `7_segment.h`, and you don't need to modify `7_segment.h`. Or just accept you can't do what you want to do (without using ugly macros that cause other problems). – Jonathan Wakely Sep 11 '14 at 16:29
  • Ok. Since I don't want to use macros I used a vector instead of a array since the vector class has a array like interface and does not require the size at compile time. I'm not entirely happy about this but it works. – BrainStone Sep 11 '14 at 16:36