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 文件中定义它?


基础教程推荐
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 常量变量在标题中不起作用 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01