Declare array in C++ header and define it in cpp file?(在 C++ 标头中声明数组并在 cpp 文件中定义它?)
问题描述
这可能是一件非常简单的事情,但我是 C++ 新手,所以需要帮助.
This is probably a really simple thing but I'm new to C++ so need help.
我只想在我的 C++ 头文件中声明一个数组,例如:
I just want to declare an array in my C++ header file like:
int lettersArr[26];
然后在 cpp 文件中的函数中定义它,例如:
and then define it in a function in the cpp file like:
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 };
但这不起作用.
是我的语法错误还是什么?正确的方法是什么?
Have I got the syntax wrong or something? What is the correct way to to this?
非常感谢.
推荐答案
在头文件的声明中添加extern
.
Add extern
to the declaration in the header file.
extern int lettersArr[26];
(另外,除非您打算更改数组,否则请考虑添加 const
.)
(Also, unless you plan to change the array, consider also adding const
.)
定义必须有类型.添加int
(或const int
):
The definition must have a type. Add int
(or const int
):
int lettersArr[26] = { letA, /*...*/ };
这篇关于在 C++ 标头中声明数组并在 cpp 文件中定义它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C++ 标头中声明数组并在 cpp 文件中定义它?


- STL 中有 dereference_iterator 吗? 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- C++ 协变模板 2021-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 近似搜索的工作原理 2021-01-01
- 静态初始化顺序失败 2022-01-01