如何让我的字符数组常数? [英] How do I make my char array a constant?

查看:127
本文介绍了如何让我的字符数组常数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C / C ++小白在这里。我在头文件中定义这个...

 的typedef unsigned char型BitChar [9]; // 8个数据位,1位宽度EXTERN BitChar BitFont [];

和我有这样一个CPP文件...

  BitChar BitFont [] = {
    B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,2,// 32 - 空间
    B10000000,B10000000,B10000000,B10000000,B10000000,B00000000,B10000000,B00000000,1,// 33 - !
    ...
    B00000000,B00000000,B11100000,B11100000,B11100000,B00000000,B00000000,B00000000,3,// 127 - 未知
};

据编译和运行似乎只是罚款。然而,因为它永远不会改变,我认为应该把它标记为一个常数。我该如何将其标记为这样?我所料,添加'常量',抛出编译错误,所以我很为难。这里的错误...

 错误:类型的引用无效初始化unsigned char型(安培)[9]从类型前pressionconst的无符号字符[9]


解决方案

只需添加常量。这

 的extern const的BitChar BitFont [];
...
常量BitChar BitFont [] = {
    B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,2,// 32 - 空间
    B10000000,B10000000,B10000000,B10000000,B10000000,B00000000,B10000000,B00000000,1,// 33 - !
    ...
    B00000000,B00000000,B11100000,B11100000,B11100000,B00000000,B00000000,B00000000,3,// 127 - 未知
};

应该工作在C完美的罚款(假设你的编译器知道这些 B00000000 标识符的意思。)

这也将工作用C完美的罚款++。在C ++版本错误的唯一潜在基于C ++ - 常量的特定属性。若定义不看的声明,那么你必须指定明确的的extern 中的定义以及

 的extern const的BitChar BitFont [] = {
    B00000000
    ...

因为在C ++ 常量对象具有的内部的默认联动。但是,如果声明中已经包含了的extern 和定义可以看到声明,那么的extern 中的定义是可选的。

您引用的错误信息显示在你的code某处您尝试初始化类型的引用 BitChar&安培; (又名 unsigned char型(安培)[9] 用const限定 BitChar 数组)。这是行不通的,因为它违反了常量,正确性的基本规则。基准有可能成为const限定为好,也就是说,它必须改变以常量BitChar&安培; (又名 const的无符号字符(安培;) 9] )。

C/C++ noob here. I've defined this in a header file...

typedef unsigned char BitChar[9]; // 8 data bits and one width bit

extern BitChar BitFont[];

and I have this in a cpp file...

BitChar BitFont[] = {
    B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,2, // 32 - Space
    B10000000,B10000000,B10000000,B10000000,B10000000,B00000000,B10000000,B00000000,1, // 33 - !
    ...
    B00000000,B00000000,B11100000,B11100000,B11100000,B00000000,B00000000,B00000000,3, // 127 - Unknown
};

It compiles and seemingly runs just fine. However, since it will never change, I thought it should be marked as a constant. How do I mark it as such? What I expected, adding 'const', throws compile errors so I'm stumped. Here's the error...

error: invalid initialization of reference of type 'unsigned char (&)[9]' from expression of type 'const unsigned char [9]' 

解决方案

Just add const. This

extern const BitChar BitFont[];
...
const BitChar BitFont[] = {
    B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,2, // 32 - Space
    B10000000,B10000000,B10000000,B10000000,B10000000,B00000000,B10000000,B00000000,1, // 33 - !
    ...
    B00000000,B00000000,B11100000,B11100000,B11100000,B00000000,B00000000,B00000000,3, // 127 - Unknown
};

should work perfectly fine in C. (Assuming that your compiler knows what these B00000000 identifiers mean.)

This will also work perfectly fine in C++. The only potential for error in the C++ version is based on C++-specific properties of const. If the definition does not see the declaration, then you have to specify the explicit extern in the definition as well

extern const BitChar BitFont[] = {
    B00000000
    ...

because in C++ const objects have internal linkage by default. However, if the declaration already contains the extern and the definition can see the declaration, then that extern in the definition is optional.

The error message you quoted suggests that somewhere in your code you are trying to initialize a reference of type BitChar & (aka unsigned char (&)[9]) with a const-qualified BitChar array. This will not work, since it violates the basic rules of const-correctness. The reference has to become const-qualified as well, i.e. it has to change to const BitChar & (aka const unsigned char (&)[9]).

这篇关于如何让我的字符数组常数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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