在 C++ 头文件中声明数组并在 cpp 文件中定义它? [英] Declare array in C++ header and define it in cpp file?

查看:62
本文介绍了在 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 文件中定义它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆