简体   繁体   中英

c++ - Defining const variable not working across header file

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM