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 文件中定义它?
基础教程推荐
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- Windows Media Foundation 录制音频 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01