Declare array in C++ header and define it in cpp file?
Add extern
to the declaration in the header file.
extern int lettersArr[26];
(Also, unless you plan to change the array, consider also adding const
.)
The definition must have a type. Add int
(or const int
):
int lettersArr[26] = { letA, /*...*/ };
Header:
extern int lettersArr[];
Source at the global scope:
int lettersArr[26] = { letA, letB, letC, letD, letE, letF, letG, letH, letI, letJ, letK, letL, letM, letN, letO, letP, letQ, letR, letS, letT, letU, letV, letW, letX, letY, letZ };
or if you really want to do it in a function:
Source at global scope:
int lettersArr[26];
Source in function:
int localLettersArr[26] = { letA, letB, letC, letD, letE, letF, letG, letH, letI, letJ, letK, letL, letM, letN, letO, letP, letQ, letR, letS, letT, letU, letV, letW, letX, letY, letZ };memcpy (lettersArr, localLettersArr, sizeof (localLettersArr));
Change what you have in the header to:
extern int lettersArr[26];
so that it will become a declaration, not a definition.